gpt4 book ai didi

android - 仅在 Kitkat 上运行我的应用程序

转载 作者:行者123 更新时间:2023-11-29 23:52:06 26 4
gpt4 key购买 nike

我希望我的应用程序仅在 Kitkat 版本上运行,而不是在任何其他 android 版本(既不旧也不新)上运行,我在应用程序级 build.gradle 文件中所做的以下更改:

android {
compileSdkVersion 19
defaultConfig {
applicationId "com.app.tempapp"
minSdkVersion 19
targetSdkVersion 19
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

但这行不通..

最佳答案

您需要在两个层面上处理它:

Google Play 商店级别

您可以使用 android:maxSdkVersion .这是一个整数,指定应用程序设计运行的最高 API 级别。如果应用的maxSdkVersion属性低于系统自身使用的API Level,那么系统将不允许安装该应用。

将此属性的值设置为 19。

defaultConfig {
minSdkVersion 19
maxSdkVersion 19
targetSdkVersion 19
...
}

注意:

此方法仅在用户通过 Play 商店安装应用时才有效。

Future versions of Android (beyond Android 2.0.1) will no longer check or enforce the maxSdkVersion attribute during installation or re-validation. Google Play will continue to use the attribute as a filter, however, when presenting users with applications available for download.

应用级别

剩下的唯一选择就是在代码中强制执行它。但缺点是,用户将能够安装该应用程序,但它只会自行停止。你可以优雅地处理它,如下所示:

if (android.os.Build.VERSION.SDK_INT != android.os.Build.VERSION_CODES.KITKAT){
final AlertDialog compatibility = new AlertDialog.Builder(this).setMessage("This app only be executed on Kitkat version")
.setPositiveButton("OK", null).create();
compatibility.setCancelable(false);
compatibility.setCanceledOnTouchOutside(false);
compatibility.show();
compatibility.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compatibility.dismiss();
finish();
}
});

}

onResume() 中添加这段代码。您可能想要添加一些额外的检查,以防用户只是最小化应用程序并再次打开,并在 onDestroy() 中关闭对话框以避免内存泄漏导致的崩溃

关于android - 仅在 Kitkat 上运行我的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50791477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com