7

我想要使用最新的融合位置提供程序客戶端獲取位於「後臺服務」上的位置更新。我不想使用所有正在使用的位置偵聽器和Google API客戶端。 我還需要使用google play服務提供的位置設置API來檢查位置設置是禁用還是啓用該「後臺服務」。請幫助。使用谷歌播放服務和FusedLocationProviderClient的位置更新

+1

你到目前爲止嘗試過什麼,你在哪裏堅持? – azizbekian

+0

請同時提及你的研究來解決這個問題 –

+0

我已經有位置更新使用位置偵聽器-LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,locationListener);但我想按照谷歌-mFusedLocationClient.requestLocationUpdates(mLocationRequest,mLocationCallback,回調) –

回答

1

可以擴展服務類像

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

您可以點擊這裏完整的源代碼BackgroundLocationService.java

對於啓用位置設置

private void displayLocationSettingsRequest() { 
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context) 
      .addApi(LocationServices.API).build(); 
    googleApiClient.connect(); 

    LocationRequest locationRequest = LocationRequest.create(); 
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    locationRequest.setInterval(10000); 
    locationRequest.setFastestInterval(10000/2); 

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest); 
    builder.setAlwaysShow(true); 

    PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build()); 
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
     @Override 
     public void onResult(@NonNull LocationSettingsResult result) { 
      final Status status = result.getStatus(); 
      switch (status.getStatusCode()) { 
       case LocationSettingsStatusCodes.SUCCESS: 
        break; 
       case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 

        try { 
         // Show the dialog by calling startResolutionForResult(), and check the result 
         // in onActivityResult(). 
         status.startResolutionForResult(context, REQUEST_CHECK_SETTINGS); 
        } catch (IntentSender.SendIntentException ignored) { 
        } 
        break; 
       case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
        break; 
      } 
     } 
    }); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
    // Check for the integer request code originally supplied to 
    startResolutionForResult(). 
     case REQUEST_CHECK_SETTINGS: 
      switch (resultCode) { 
       case Activity.RESULT_OK: 
        //startLocationUpdates(); 
        break; 
       case Activity.RESULT_CANCELED: 
        displayLocationSettingsRequest();//keep asking 
        showToast("Location permission needed"); 
        break; 
      } 
      break; 
    } 
} 

您可以從服務檢查的位置設置與如果位置關閉,

LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); 
    boolean gps_enabled = false; 
     boolean network_enabled = false; 

    try { 
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 
} catch(Exception ex) {} 

    try { 
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
    } catch(Exception ex) {} 

    if(!gps_enabled && !network_enabled) { 
// notify user 
// Either you can display a Notification(Recommended way) or you can show dialog with 

dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 
//It needs SYSTEM_ALERT_WINDOW permission. Add this permission in Manifest. 

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>  

} 
+0

您正在檢查活動的設置,我的問題是如何在沒有前臺活動時檢查服務的設置。 –

+0

查看我的更新回答 – Ameer

+0

從哪裏可以得到該位置設置窗口的結果,這樣我可以在位置設置打開時請求位置更新@Ameer –

1

您可以編寫intentService的代碼,如下所示。一直認爲,由於它在後臺運行,我們不會顯示權限對話框。

public class GetLocationService extends IntentService 
    implements 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, LocationListener { 
public static String TAG = GetLocationService.class.getSimpleName(); 
private GoogleApiClient mGoogleApiClient; 


public GetLocationService() { 
    super(TAG); 
} 

/** 
* Creates an IntentService. Invoked by your subclass's constructor. 
* 
* @param name Used to name the worker thread, important only for debugging. 
*/ 
public GetLocationService(String name) { 
    super(name); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) { 
     Log.d(TAG, "disconnecting"); 
     mGoogleApiClient.disconnect(); 
    } 
    Log.d(TAG, "onHandleIntent"); 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 
    mGoogleApiClient.connect(); 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    LocationRequest locationRequest = LocationRequest.create(); 
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

    LocationRequest locationRequestLow = LocationRequest.create(); 
    locationRequestLow.setPriority(LocationRequest.PRIORITY_LOW_POWER); 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == 
      PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this); 
    } else { 
     Log.d(TAG, "permission denied"); 
    } 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

@Override 
public void onLocationChanged(Location location) { 
    Log.d(TAG, location.toString());  
} 

}

2

,如果我們需要使用融合定位API,那麼我們就需要使用谷歌API客戶端,對於後臺服務是不使用它的問題,以下是我使用的例子讓在後臺位置更新,但有一件事是谷歌推出「打盹」從6.0模式,所以我不能給保證人給予更新,當您的設備在「打盹」模式,

import android.Manifest; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.os.Build; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.IBinder; 
import android.support.annotation.NonNull; 
import android.support.annotation.Nullable; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.util.Log; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GoogleApiAvailability; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.location.LocationServices; 

/** 
* Created By Dhaval Solanki 
*/ 

public class ConnectionService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { 
    String TAG = "ConnectionService"; 


    private GoogleApiClient mGoogleApiClient; 
    private LocationRequest mLocationRequest; 
    private Location previousLocation; 
    public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 30000; 
    public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = 
      UPDATE_INTERVAL_IN_MILLISECONDS/2; 


    private Location mLastLocation; 
    private Context context; 

    @Override 
    public IBinder onBind(Intent intent) { 
     Log.i(TAG, "onBind"); 
     return null; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     context = getApplicationContext(); 
     Log.i(TAG, "onStartCommand"); 
     if (checkPermsion(context)) { 
      setupLocationService(context); 
     } 
     return Service.START_STICKY; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     ApplicationCLass.isServiceRunning = true; 
    } 

    @Override 
    public boolean onUnbind(Intent intent) { 
     Log.i(TAG, "onUnbind"); 
     ApplicationCLass.isServiceRunning = false; 
     return super.onUnbind(intent); 
    } 

    @Override 
    public void onDestroy() { 
     ApplicationCLass.isServiceRunning = false; 
     super.onDestroy(); 
    } 

    private void setupLocationService(Context context) { 
     if (checkPlayServices()) { 
      mGoogleApiClient = new GoogleApiClient.Builder(context) 
        .addApi(LocationServices.API) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .build(); 
      createLocationRequest(); 
     } 
    } 

    protected void createLocationRequest() { 
     mLocationRequest = new LocationRequest().create(); 
     mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS); 
     mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
     mGoogleApiClient.connect(); 
    } 

    public boolean checkPermsion(Context context) { 
     int MyVersion = Build.VERSION.SDK_INT; 
     if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) { 
      if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       return false; 
      } else if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       return false; 
      } else { 
       return true; 
      } 
     } else { 
      return true; 
     } 
    } 

    private boolean checkPlayServices() { 
     GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance(); 
     int result = googleAPI.isGooglePlayServicesAvailable(this); 
     if (result != ConnectionResult.SUCCESS) { 
      return false; 
     } 
     return true; 
    } 


    private void startLocationUpdates() { 
     if (mGoogleApiClient.isConnected()) { 
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       return; 
      } 
      if (Prefs.getUserLat(context).trim().length() > 0 && Prefs.getUserLong(context).trim().length() > 0) { 
      } else { 
       mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
         mGoogleApiClient); 
       if (mLastLocation != null) { 
        Prefs.setUserLat(context, String.valueOf(mLastLocation.getLatitude())); 
        Prefs.setUserLong(context, String.valueOf(mLastLocation.getLongitude())); 
       } 
      } 
      LocationServices.FusedLocationApi.requestLocationUpdates(
        mGoogleApiClient, mLocationRequest, this); 
     } 
    } 

    @Override 
    public void onConnected(@Nullable Bundle bundle) { 
     Log.i(TAG, "Connected to onConnected"); 
     startLocationUpdates(); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     Log.i(TAG, "Connected to onConnectionSuspended"); 
    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
     Log.i(TAG, "Connected to onConnectionFailed"); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     try { 
      Logger.print("onLocationChanged", "latitued :" + location.getLatitude() + " ,, Longitude " + location.getLongitude()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 


} 

彼此alaram服務繼續捉迷藏或不是

import android.app.ActivityManager; 
import android.app.IntentService; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 

public class AlarmService extends IntentService { 
    @Override 
    protected void onHandleIntent(Intent intent) { 
     boolean isServiceRunning = false; 
     ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 
     for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { 
      if (ConnectionService.class.getName().equals(service.service.getClassName())) { 
       isServiceRunning = true; 
       break; 
      } 
     } 
     Log.i("AlarmService", "Service is running " + isServiceRunning); 
     if(!isServiceRunning) { 
      startService(new Intent(getApplicationContext(),ConnectionService.class)); 
     } else { 
      ConnectionService.checkConnectionAndConnect(getApplicationContext()); 
     } 
    } 

    /** 
    * Creates an IntentService. Invoked by your subclass's constructor. 
    */ 
    public AlarmService() { 
     super("AlaramService"); 
    } 
} 

而且Finaly啓動服務

private void checkAndStartService() { 
     final ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 
     new AsyncTask<Void, Void, Boolean>() { 
      boolean isServiceRunning = false; 

      @Override 
      protected Boolean doInBackground(Void... params) { 
       for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { 
        if (ConnectionService.class.getName().equals(service.service.getClassName())) { 
         isServiceRunning = true; 
         break; 
        } 
       } 
       return isServiceRunning; 
      } 

      @Override 
      protected void onPostExecute(Boolean aBoolean) { 
       super.onPostExecute(aBoolean); 
       Log.i("onPostExecute", "Service running = " + aBoolean); 
       if (!aBoolean) { 
        startService(new Intent(ActivitySplash.this, ConnectionService.class)); 
        Intent i = new Intent(ActivitySplash.this, AlarmService.class); 
        PendingIntent pi = PendingIntent.getService(ActivitySplash.this, 0, i, 0); 
        AlarmManager am = (AlarmManager) ActivitySplash.this.getSystemService(Context.ALARM_SERVICE); 
        am.cancel(pi); // cancel any existing alarms 
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60000, pi); 
       } 
      } 
     }.execute(); 
    } 
+0

只是一個問題,如果用戶在服務運行時關閉gps,那麼如何才能你得到的位置更新@Dhaval Solanki –

+0

切換後它不可能我說的待機模式意味着鎖定手機。 –

+0

如果確實關閉並啓動開關,則需要重新啓動可使用啓動完成接收器實現的服務。 –

1

您好我有關於位置的服務很好的理解,以及我們如何能在這完美的方式使用。看看我會給你關於融合apis的想法。

只要確保按照以下步驟。這很不錯,很簡單。 第1步。這個類 GoogleLocationService.java

public class GoogleLocationService { 
private GoogleServicesCallbacks callbacks = new GoogleServicesCallbacks(); 
LocationUpdateListener locationUpdateListener; 
Context activity; 
protected GoogleApiClient mGoogleApiClient; 
protected LocationRequest mLocationRequest; 

public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 30000; 


public GoogleLocationService(Context activity, LocationUpdateListener locationUpdateListener) { 
    this.locationUpdateListener = locationUpdateListener; 
    this.activity = activity; 
    buildGoogleApiClient(); 
} 

protected synchronized void buildGoogleApiClient() { 
    //Log.i(TAG, "Building GoogleApiClient"); 
    mGoogleApiClient = new GoogleApiClient.Builder(activity) 
      .addConnectionCallbacks(callbacks) 
      .addOnConnectionFailedListener(callbacks) 
      .addApi(LocationServices.API) 
      .build(); 
    createLocationRequest(); 
    mGoogleApiClient.connect(); 
} 

protected void createLocationRequest() { 
    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

} 

private class GoogleServicesCallbacks implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { 

    @Override 
    public void onConnected(Bundle bundle) { 
     startLocationUpdates(); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

     if (connectionResult.getErrorCode() == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED) { 
      Toast.makeText(activity, "Google play service not updated", Toast.LENGTH_LONG).show(); 

     } 
     locationUpdateListener.cannotReceiveLocationUpdates(); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     if (location.hasAccuracy()) { 
      if (location.getAccuracy() < 30) { 
       locationUpdateListener.updateLocation(location); 
      } 
     } 
    } 
} 

private static boolean locationEnabled(Context context) { 
    boolean gps_enabled = false; 
    LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
    try { 
     gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
    return gps_enabled; 
} 

private boolean servicesConnected(Context context) { 
    return isPackageInstalled(GooglePlayServicesUtil.GOOGLE_PLAY_STORE_PACKAGE, context); 
} 

private boolean isPackageInstalled(String packagename, Context context) { 
    PackageManager pm = context.getPackageManager(); 
    try { 
     pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES); 
     return true; 
    } catch (PackageManager.NameNotFoundException e) { 
     e.printStackTrace(); 
     return false; 
    } 
} 


public void startUpdates() { 
    /* 
    * Connect the client. Don't re-start any requests here; instead, wait 
    * for onResume() 
    */ 
    if (servicesConnected(activity)) { 
     if (locationEnabled(activity)) { 
      locationUpdateListener.canReceiveLocationUpdates(); 
      startLocationUpdates(); 
     } else { 
      locationUpdateListener.cannotReceiveLocationUpdates(); 
      Toast.makeText(activity, "Unable to get your location.Please turn on your device Gps", Toast.LENGTH_LONG).show(); 
     } 
    } else { 
     locationUpdateListener.cannotReceiveLocationUpdates(); 
     Toast.makeText(activity, "Google play service not available", Toast.LENGTH_LONG).show(); 
    } 
} 

//stop location updates 
public void stopUpdates() { 
    stopLocationUpdates(); 
} 

//start location updates 
private void startLocationUpdates() { 

    if (checkSelfPermission(activity, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(activity, ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     return; 
    } 
    if (mGoogleApiClient.isConnected()) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, callbacks); 
    } 
} 

public void stopLocationUpdates() { 
    if (mGoogleApiClient.isConnected()) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, callbacks); 
    } 
} 

public void startGoogleApi() { 
    mGoogleApiClient.connect(); 
} 

public void closeGoogleApi() { 
    mGoogleApiClient.disconnect(); 
} 

} 

第二步。使此接口 LocationUpdateListener.java

public interface LocationUpdateListener { 

/** 
* Called immediately the service starts if the service can obtain location 
*/ 
void canReceiveLocationUpdates(); 

/** 
* Called immediately the service tries to start if it cannot obtain location - eg the user has disabled wireless and 
*/ 
void cannotReceiveLocationUpdates(); 

/** 
* Called whenever the location has changed (at least non-trivially) 
* @param location 
*/ 
void updateLocation(Location location); 

/** 
* Called when GoogleLocationServices detects that the device has moved to a new location. 
* @param localityName The name of the locality (somewhere below street but above area). 
*/ 
void updateLocationName(String localityName, Location location); 
} 

第3步:讓這個位置服務類 LocationService。java的

public class LocationService extends Service { 

private GoogleLocationService googleLocationService; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    //start the handler for getting locations 
    //create component 

    updateLocation(getApplicationContext()); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    return Service.START_STICKY; 
} 

//get current location os user 
private void updateLocation(Context context) { 
    googleLocationService = new GoogleLocationService(context, new LocationUpdateListener() { 
     @Override 
     public void canReceiveLocationUpdates() { 
     } 

     @Override 
     public void cannotReceiveLocationUpdates() { 
     } 

     //update location to our servers for tracking purpose 
     @Override 
     public void updateLocation(Location location) { 
      if (location != null) { 
       Timber.e("updated location %1$s %2$s", location.getLatitude(), location.getLongitude()); 

      } 
     } 

     @Override 
     public void updateLocationName(String localityName, Location location) { 

      googleLocationService.stopLocationUpdates(); 
     } 
    }); 
    googleLocationService.startUpdates(); 
} 


IBinder mBinder = new LocalBinder(); 


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

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

//stop location updates on stopping the service 
@Override 
public void onDestroy() { 
    super.onDestroy(); 
    if (googleLocationService != null) { 
     googleLocationService.stopLocationUpdates(); 
    } 
} 
} 

編輯答案:

如何使用服務,您的主要活動

啓動服務,這樣

startService(new Intent(context, LocationService.class)); 

停,

stopService(new Intent(context, LocationService.class)); 

,並在這樣的清單申報,

<service 
android:name=".service.location.LocationService" 
android:enabled="true"></service> 

注:我已經這樣做了,因爲我需要的位置後殺死你不想service.Then的app.If,可以在下面的代碼直接調用在需要更新位置的類中,並刪除位置服務。

private GoogleLocationService googleLocationService; 

googleLocationService = new GoogleLocationService(context, new LocationUpdateListener() { 
    @Override 
    public void canReceiveLocationUpdates() { 
    } 

    @Override 
    public void cannotReceiveLocationUpdates() { 
    } 

    //update location to our servers for tracking purpose 
    @Override 
    public void updateLocation(Location location) { 
     if (location != null) { 
      Timber.e("updated location %1$s %2$s", location.getLatitude(), location.getLongitude()); 

     } 
    } 

    @Override 
    public void updateLocationName(String localityName, Location location) { 

     googleLocationService.stopLocationUpdates(); 
    } 
}); 
googleLocationService.startUpdates(); 


and call this onDestroy 
if (googleLocationService != null) { 
    googleLocationService.stopLocationUpdates(); 
} 

記住LocationService也應該在manifest中聲明。 據我所知,這是最好的解決方案。 謝謝,希望這會幫助你

0

這個問題似乎不是很具體。看起來你開始使用位置服務時遇到問題。

最好的想法可能是查看Google示例(https://github.com/googlesamples/android-play-location)以更好地理解機制。 我可能會從實施最簡單的例子開始(LocationUpdates)。它使用FusedLocationProvider,並檢查設置是否啓用,因此它應該適合您的需求,或者至少讓您開始。

如果你真的需要使用服務,你可以檢查出LocationUpdatesForegroundServiceLocationUpdatesPendingIntent。但是,我強烈建議在閱讀並理解第一個示例之前,先執行此操作。

相關問題