如何使用 startResolutionForResult 获得 activity 服务

how to get activity in service using startResolutionForResult

我想启动一个服务,如果它在设备启动过程中关闭,它会打开 GPS 对话。我在启动时有一个广播接收器然后我触发此服务以打开对话而不打开主application.I正在使用服务,在服务中如果禁用我需要调用 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); 如何在服务中处理它?因为我们不能在这里传递 activity。

in service I need to call GPS location dialogue box to turn on if disable

这是不可能的,抱歉。在开始服务之前,请在您的 activity 中完成这项工作。如果该服务检测到用户在服务 运行 时禁用了位置访问,则该服务可以引发 Notification,将用户引导至 activity,您可以在其中请求 GPS 访问。