首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用startResolutionForResult获取服务中的活动

如何使用startResolutionForResult获取服务中的活动
EN

Stack Overflow用户
提问于 2018-03-31 13:11:56
回答 1查看 726关注 0票数 0

我想启动一个服务,如果它是在设备启动进程上关闭的话,就会打开GPS对话。我有一个广播接收器启动,然后我触发这个服务打开对话,而不打开主应用程序,我使用的服务,在服务中,我需要调用GPS位置对话框打开,如果禁用。

代码语言:javascript
运行
复制
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)有一个问题;如何在服务中处理它?因为我们不能在这里进行活动。

EN

Stack Overflow用户

回答已采纳

发布于 2018-03-31 13:40:07

在服务中,如果禁用,我需要调用GPS位置对话框来打开。

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

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49588130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档