2014-06-07 5 views
0

백그라운드에서 사용자 위치를 얻으려고합니다. 모든 내 전화 (HTC 하나 M7)에 큰 작동하지만 어떤 이유에서 내가 테스트를 두 장치에서 작동하지 않습니다 : BTW 삼성 갤럭시 S3 소니 엑스 페리아 Z1LocationClient는 일부 기기에서만 작동합니다.

을 : 나는 그것이 있어야로 매니페스트에 모든 것을 추가 . ** BackgroundLocationService **

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 

import android.app.PendingIntent; 
import android.app.Service; 
import android.content.Intent; 
import android.os.Binder; 
import android.os.Bundle; 
import android.os.IBinder; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesClient; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.location.LocationClient; 
import com.google.android.gms.location.LocationRequest; 

public class BackgroundLocationService extends Service implements 
    GooglePlayServicesClient.ConnectionCallbacks, 
    GooglePlayServicesClient.OnConnectionFailedListener { 

IBinder mBinder = new LocalBinder(); 

private LocationClient mLocationClient; 
private LocationRequest mLocationRequest; 
// Flag that indicates if a request is underway. 
private boolean mInProgress; 

private Boolean servicesAvailable = false; 

public class LocalBinder extends Binder { 
    public BackgroundLocationService getServerInstance() { 
     return BackgroundLocationService.this; 
    } 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 


    mInProgress = false; 
    // Create the LocationRequest object 
    mLocationRequest = LocationRequest.create(); 
    // Use high accuracy 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
    // Set the update interval to 5 seconds 
    mLocationRequest.setInterval(0); 
    // Set the fastest update interval to 1 second 
    mLocationRequest.setFastestInterval(1); 

    servicesAvailable = servicesConnected(); 

    /* 
    * Create a new location client, using the enclosing class to 
    * handle callbacks. 
    */ 
    mLocationClient = new LocationClient(this, this, this); 


} 

private boolean servicesConnected() { 

    // Check that Google Play services is available 
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 

    // If Google Play services is available 
    if (ConnectionResult.SUCCESS == resultCode) { 

     return true; 
    } else { 

     return false; 
    } 
} 

public int onStartCommand (Intent intent, int flags, int startId) 
{ 
    super.onStartCommand(intent, flags, startId); 

    if(!servicesAvailable || mLocationClient.isConnected() || mInProgress) 
     return START_STICKY; 

    setUpLocationClientIfNeeded(); 
    if(!mLocationClient.isConnected() || !mLocationClient.isConnecting() && !mInProgress) 
    { 
     mInProgress = true; 
     mLocationClient.connect(); 
    } 

    return START_STICKY; 
} 

/* 
* Create a new location client, using the enclosing class to 
* handle callbacks. 
*/ 
private void setUpLocationClientIfNeeded() 
{ 
    if(mLocationClient == null) 
     mLocationClient = new LocationClient(this, this, this); 
} 

// Define the callback method that receives location updates 


@Override 
public IBinder onBind(Intent intent) { 
    return mBinder; 
} 



public void appendLog(String text, String filename) 
{  
    File logFile = new File(filename); 
    if (!logFile.exists()) 
    { 
     try 
     { 
     logFile.createNewFile(); 
     } 
     catch (IOException e) 
     { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
    } 
    try 
    { 
     //BufferedWriter for performance, true to set append to file flag 
     BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
     buf.append(text); 
     buf.newLine(); 
     buf.close(); 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

@Override 
public void onDestroy(){ 
    // Turn off the request flag 
    mInProgress = false; 
    if(servicesAvailable && mLocationClient != null) { 
     //mLocationClient.removeLocationUpdates(callbackIntent); 
     // Destroy the current location client 
     mLocationClient = null; 
    } 
    // Display the connection status 
    // Toast.makeText(this, DateFormat.getDateTimeInstance().format(new Date()) + ": Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show(); 
    super.onDestroy(); 
} 

/* 
* Called by Location Services when the request to connect the 
* client finishes successfully. At this point, you can 
* request the current location or start periodic updates 
*/ 
@Override 
public void onConnected(Bundle bundle) { 

    // Request location updates using static settings 
    Intent intent = new Intent(this, LocationReceiver.class); 
     PendingIntent locationIntent = PendingIntent.getBroadcast(getApplicationContext(), 14872, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     mLocationClient.requestLocationUpdates(mLocationRequest, locationIntent); 
} 

/* 
* Called by Location Services if the connection to the 
* location client drops because of an error. 
*/ 
@Override 
public void onDisconnected() { 
    // Turn off the request flag 
    mInProgress = false; 
    // Destroy the current location client 
    mLocationClient = null; 
    // Display the connection status 
    // Toast.makeText(this, DateFormat.getDateTimeInstance().format(new Date()) + ": Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show(); 
} 

/* 
* Called by Location Services if the attempt to 
* Location Services fails. 
*/ 
@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    mInProgress = false; 

    /* 
    * Google Play services can resolve some errors it detects. 
    * If the error has a resolution, try sending an Intent to 
    * start a Google Play services activity that can resolve 
    * error. 
    */ 
    if (connectionResult.hasResolution()) { 

    // If no resolution is available, display an error dialog 
    } else { 

    } 
} 


} 

** LocationReceiver **

import java.util.List; 
import java.util.Locale; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.Location; 
import android.util.Log; 

import com.google.android.gms.location.LocationClient; 

public class LocationReceiver extends BroadcastReceiver { 
public static double lat; 
public static double alt; 
public static String address; 


@Override 
public void onReceive(Context context, Intent intent) { 

    Location location = (Location) intent.getExtras().get(LocationClient.KEY_LOCATION_CHANGED); 
    Log.d("New Location Reciver", "location "+location.toString()); 

    lat = location.getLatitude(); 
    alt = location.getAltitude(); 
    address = getAddressByCord(lat, alt, context); 
} 


public static String getAddressByCord(double lat, double longi, Context context) { 

    try { 
     Geocoder geo = new Geocoder(context, Locale.getDefault()); 
     List<Address> addresses = geo.getFromLocation(lat, longi, 1); 
     if (addresses.isEmpty()) { 
      return "Waiting for Location"; 
     } else { 
      if (addresses.size() > 0) { 
       String s = ""; 
       if (addresses.get(0).getFeatureName() != null) 
        s += addresses.get(0).getFeatureName(); 
       if (addresses.get(0).getThoroughfare() != null) 
        s += "," +   addresses.get(0).getThoroughfare(); 
       if (addresses.get(0).getLocality() != null) 
        s += "," + addresses.get(0).getLocality(); 
       if (addresses.get(0).getAdminArea() != null) 
        s += "," + addresses.get(0).getAdminArea(); 
       if (addresses.get(0).getCountryName() != null) 
        s += "," + addresses.get(0).getCountryName(); 
       return s; 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return ""; 
} 
} 

사람이 어떤 생각을 가지고 있다면, 당신을 감사합니다

이 내 코드입니다!

+0

LocationClient에는 Google Play 서비스가 필요합니다. 장치에 장치가 있는지 확인할 수 있습니까? –

+0

예 확인했지만 모든 4.4 장치에있는 것은 아닙니다. 내가 사용하는 장치는 4.4입니다. – Fashizel

+0

모두들 가지고 있어야합니다. OS 버전은 언급하지 않았습니다. 플레이 서비스를 제거 할 수 있습니다. –

답변

0

답변을 찾았습니다. 장치 자체 정의였습니다. 기기가 앱에서 위치 정보를 사용하도록 설정하지 못했습니다.

어쨌든 감사합니다!

+0

playSvices가 표시되는 방법을 말해 줄 수 있습니까? 서비스 ? – Erum

0

Google Play 서비스를 확인하는 코드입니다.

private static final int CONNECTION_FAILURE_RESOLUTION_REQUEST = 94378; 

private boolean isGooglePlay() { 
    int mIsGooglePlayServicesAvailable = GooglePlayServicesUtil 
      .isGooglePlayServicesAvailable(context); 

    switch (mIsGooglePlayServicesAvailable) { 
    case ConnectionResult.SUCCESS: 
     return true; 
    default: 
     Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(
       mIsGooglePlayServicesAvailable, this, 
       CONNECTION_FAILURE_RESOLUTION_REQUEST); 
     // If Google Play services can provide an error dialog 
     if (errorDialog != null) { 
      // Create a new DialogFragment for the error dialog 
      ErrorDialogFragment errorFragment = new ErrorDialogFragment(); 
      // Set the dialog in the DialogFragment 
      errorFragment.setDialog(errorDialog); 
      // Show the error dialog in the DialogFragment 
      errorFragment.show(getSupportFragmentManager(), 
        "Location Updates"); 
     } 
     // case ConnectionResult.SERVICE_MISSING: 
     // case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED: 
     // case ConnectionResult.SERVICE_DISABLED: 
     // case ConnectionResult.SERVICE_INVALID: 
     // case ConnectionResult.DATE_INVALID: 
    } 
    return false; 
} 

/* 
* This is a class used to resolve errors with google play services It is 
* copied code that doesn't run durring normal operation. 
*/ 
public static class ErrorDialogFragment extends DialogFragment { 
    // Global field to contain the error dialog 
    private Dialog mDialog; 

    // Default constructor. Sets the dialog field to null 
    public ErrorDialogFragment() { 
     super(); 
     mDialog = null; 
    } 

    // Set the dialog to display 
    public void setDialog(Dialog dialog) { 
     mDialog = dialog; 
    } 

    // Return a Dialog to the DialogFragment. 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     return mDialog; 
    } 
} 
+0

어떻게 서비스에 대한 코드를 작성할 수 있습니까? 내 backgroundclass가 서비스에서 확장 될 수 있습니다. googleplay 서비스를 사용할 수 없다는 것을 사용자에게보고 할 수 있고 위에 언급 된 대화 상자 위에 표시 할 수 있습니다. – Erum

관련 문제