Play服务过时后停止启动应用

时间:2018-10-17 08:13:53

标签: java android react-native google-play-services chromecast

对于我的Android应用,我将Google Play服务用于诸如Chromecast,地图和分析之类的事情。

在我的build.gradle文件中,编译版本12.0.0:

implementation "com.google.android.gms:play-services-cast-framework:12.0.0"
implementation "com.google.android.gms:play-services-cast:12.0.0"
implementation "com.google.android.gms:play-services-base:12.0.0"
implementation "com.google.android.gms:play-services-maps:12.0.0"
implementation "com.google.android.gms:play-services-analytics:12.0.0"
implementation "com.google.android.gms:play-services-vision:12.0.0"
implementation "com.google.android.gms:play-services-gcm:12.0.0"

问题是,当人们的设备上未安装12版或更高版本时,应用程序将崩溃。

所以我做了以下事情:

AndroidManifest.xml中:

 <meta-data
      android:name="com.google.android.gms.version"
      android:value="@integer/google_play_services_version"/>

还有一小堂课,用于检查播放服务是否是最新的或已安装:

public boolean checkIfInstalled(Context applicationContext) {
    try {
        GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
        int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(applicationContext);

        // Version is out of date
        if (resultCode != ConnectionResult.SUCCESS) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(applicationContext);
            builder.setMessage("Google Play Services is required to use this app. Install Play Services or update to the latest version to continue.");

            builder.setPositiveButton("Go to Play store", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                Log.i("PlayServicesCheck", "clicked on the button");
                }
            });

            AlertDialog alertDialog = builder.create();
            alertDialog.show();

            return false;
        }

        return resultCode == ConnectionResult.SUCCESS;
    } catch (Exception err) {
        return false;
    }
}

在onCreate(应用启动时)中,我调用此函数来检查是否一切正常。过时的函数将返回false。因此,我的代码现在可以正常工作了。

@Override
protected void onCreate(Bundle savedInstanceState) {

    boolean isInstalled = checkIfInstalled(getApplicationContext());

    // this works, I get the right value back from the checkIfInstalled function
    if (isInstalled) {
        Log.i("PlayServicesCheck", "Installed");
        // Do nothing
    } else {
        Log.i("PlayServicesCheck", "Not Installed");
        // Give alert, stop everything else
    }

    super.onCreate(savedInstanceState);
}

但是发生这种情况时,如何停止启动应用程序?现在它说:“版本已过时”,但它仍将继续启动该应用程序,从而导致崩溃。

谢谢

1 个答案:

答案 0 :(得分:0)

您可以致电finishAndRemoveTask

从商店检查Google将版本部署到所有服务器需要花费一些时间,因此,您可能会被迫进行更新,但是当您进入商店时仍然看不到新版本。

无论如何,最好有一台服务器返回最小版本。这将使您可以微调要强制用户下载的更改和不需要的更改。如果强制使用每个版本,有时可能会对用户造成困扰。