当用户拒绝更新Google Play服务时,您如何正确关闭活动?我正在使用makeGooglePlayServicesAvailable()
,因为它似乎很方便,但我没有找到很多使用它的例子。
我在checkGooglePlayServices()
和onCreate()
中使用onResume()
(下面的代码)。
public class MainScreen extends Activity {
private static final String TAG = "MainScreen";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkGooglePlayServices();
}
@Override
protected void onResume() {
super.onResume();
checkGooglePlayServices();
}
void checkGooglePlayServices()
{
GoogleApiAvailability.getInstance()
.makeGooglePlayServicesAvailable(this)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void ignored) {
Log.d(TAG,"makeGooglePlayServicesAvailable().onSuccess()");
// GPS available; do something useful
}
}).addOnFailureListener(this,new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG,"makeGooglePlayServicesAvailable().onFailure()");
e.printStackTrace();
Toast.makeText(MainScreen.this,
"Google Play services upgrade required",
Toast.LENGTH_SHORT).show();
// can't proceed without GPS; quit
MainScreen.this.finish(); // this causes a crash
}
});
}
}
调用finish()
时应用程序崩溃:
com.example E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example, PID: 5336
java.lang.RuntimeException: Unable to destroy activity {com.example/com.example.MainScreen}: java.lang.IllegalStateException: Task is already complete
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4438)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4456)
...
Caused by: java.lang.IllegalStateException: Task is already complete
at com.google.android.gms.common.internal.zzbo.zza(Unknown Source:8)
at com.google.android.gms.tasks.zzn.zzDH(Unknown Source:8)
at com.google.android.gms.tasks.zzn.setException(Unknown Source:9)
at com.google.android.gms.tasks.TaskCompletionSource.setException(Unknown Source:2)
...
答案 0 :(得分:2)
我认为你正在按下&#34;返回&#34;取消GPS更新请求。错误发生在对onDestroy()
超级电话的调用上。这似乎表明Android已经预计无法继续下去并且已经关闭了。 (这只是猜测。)
无论如何,我无法确定使用失败侦听器回调来关闭事物的优雅方式,但这是一种稍微不同的方法仍然使用GoogleApiAvailability
。我已经对此进行了测试,似乎有效。
将checkGooglePlayServices()
替换为以下内容:
private static final int GPS_REQUEST_CODE = 1; // arbitrary code
void checkGooglePlayServices() {
GoogleApiAvailability api = GoogleApiAvailability.getInstance();
int status = api.isGooglePlayServicesAvailable(this);
if (status != ConnectionResult.SUCCESS) {
if (api.isUserResolvableError(status)) {
// onActivityResult() will be called with GPS_REQUEST_CODE
Dialog dialog = api.getErrorDialog(this, status, GPS_REQUEST_CODE,
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// GPS update was cancelled.
// Do toast or dialog (probably better to do) here.
finish();
}
});
dialog.show();
} else {
// unrecoverable error
}
}
}
此代码可避免回调,但仍可让您检查GPS的可用性。 Here是文档。
Here与示例应用中的方法相同。
如果您想继续使用您拥有的代码,可以执行以下操作,但我更喜欢上述操作来捕获崩溃并隐藏用户的肮脏。
@Override
public void onDestroy() {
try {
super.onDestroy();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
public static void checkPlayServices(Activity activity) {
final GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int errorCode = apiAvailability.isGooglePlayServicesAvailable(activity);
if (errorCode != ConnectionResult.SUCCESS) {
if (apiAvailability.isUserResolvableError(errorCode)) {
apiAvailability.makeGooglePlayServicesAvailable(activity);
} else {
Toast.makeText(activity, R.string.push_error_device_not_compatible, Toast.LENGTH_SHORT).show();
}
}
}