我想启动一个服务,如果它是在设备启动进程上关闭的话,就会打开GPS对话。我有一个广播接收器启动,然后我触发这个服务打开对话,而不打开主应用程序,我使用的服务,在服务中,我需要调用GPS位置对话框打开,如果禁用。
public class GPSTestService extends Service implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener ,VolleyResultCallBack {
private boolean ContinueConnection = true;
LocationRequest mLocationRequest;
public static GoogleApiClient mGoogleApiClient;
PendingResult<LocationSettingsResult> result;
final static int REQUEST_LOCATION = 199;
Context contex;
@Override
public void onVolleyErrorListener(VolleyError error) {
}
@Override
public void onVolleyResultListener(Context mContext, JSONArray response) {
}
@Override
public void onCreate() {
super.onCreate();
contex=getApplicationContext();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onConnected(@Nullable Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(30 * 1000);
mLocationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
builder.setAlwaysShow(true);
result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
//final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
//...
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
Toast.makeText(getApplicationContext(), "GPSTestService", Toast.LENGTH_SHORT).show();
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult((Activity) contex, REQUEST_LOCATION);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
//...
break;
}
}
});
}
@Override
public void onConnectionSuspended(int i) {
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Utils.getInstance().syncData(this,this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
mGoogleApiClient.connect();
return Service.START_NOT_STICKY;
}}
我对status.startResolutionForResult((活动)上下文,REQUEST_LOCATION)有一个问题;如何在服务中处理它?因为我们不能在这里进行活动。
发布于 2018-03-31 13:40:07
在服务中,如果禁用,我需要调用GPS位置对话框来打开。
那是不可能的抱歉。在开始服务之前,在您的活动中进行这项工作。如果服务检测到用户在运行服务时禁用了位置访问,则该服务会引发一个Notification,该Notification将引导用户到您可以请求GPS访问的活动。
https://stackoverflow.com/questions/49588130
复制相似问题