如果在设备启动过程中关闭,我想启动一个打开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((Activity)context,REQUEST_LOCATION)的问题; 如何在服务中处理它?因为我们不能在这里传递活动。
答案 0 :(得分:1)
在服务中我需要调用GPS位置对话框以在禁用时打开
这是不可能的,对不起。在启动服务之前,在您的活动中执行此操作。如果服务检测到用户在服务运行时禁用了位置访问权限,则该服务可以引发Notification
,从而引导用户进行可以请求GPS访问的活动。