當用戶拒絕更新Google Play服務時,您如何正確關閉活動?我正在使用makeGooglePlayServicesAvailable()
,因爲它看起來很方便,但我還沒有找到很多使用它的例子。處理取消makeGooglePlayServicesAvailable
我在onCreate()
和onResume()
中使用checkGooglePlayServices()
(代碼如下)。
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)
...
感謝這似乎是檢查GPS的常用方法。如果有人有關於使用'makeGooglePlayServicesAvailable'的答案,我會留下更長的問題。你還想要檢查什麼?您鏈接的文檔僅用於檢查可用性。 – stardt
如果它可以幫助某人,解決方案獲取'E/WindowManager:android.view.WindowLeaked:Activity com.example.MainScreen泄漏了窗口...'是不會在'onCreate'中運行'checkGPS()',儘管[文檔](https://firebase.google.com/docs/cloud-messaging/android/client)說:「建議在兩個位置執行此操作:在主活動的onCreate()方法和onResume () 方法。」 – stardt
@stardt我更新了答案。對於需要檢查的其他內容,我只是在考慮檢查網絡可用性,因爲需要更新GPS。不過,我對'makeGooglePlayServicesAvailable'很好奇。它看起來像開始檢查GPS的任務。這與我在答案中描述的有何不同? – Cheticamp