编程知识 cdmana.com

[Android primary] how to make app unable to run on the specified system version

As there are more and more three parties on the market APP Appearance , Some mobile phone manufacturers are also beginning to pay attention to these APP There are installation restrictions or operation restrictions , Or three parties APP Its version is too low , Cannot be supported by a specific system version .

Today I'm going to simulate a “ because APP Its version is too low 、 Causes it to fail to run on the current version of the system ” Functional effect of .

The implementation idea is as follows :

  1. To get APP The target running version of , Also know the compiled version of the system

  2. By comparing versions , In entering the APP when , Make decisions for users “ Running... Is not supported ” A hint of

  3. After the user confirms the prompt , Exit the program directly APP

The key point is targetSdkVersion Use , Source code is as follows :

1、 Lord Activity

public class TargetSdkVersionDemo extends Activity {
    @Override
    protected void onCreate(Bundle onSavedInstance) {
        super.onCreate(onSavedInstance);
        setContentView(R.layout.target_sdk_version_demo);

        int appTargetSdkVersion = getApplicationInfo().targetSdkVersion;
        Log.i("TargetSdkVersionDemo", "targetsdkversion " + appTargetSdkVersion);
        
        //  Let's assume this APP The target running version of is no higher than  API Level 26
        if (appTargetSdkVersion <= Build.VERSION_CODES.O) {
            showNotSupportDialog(appTargetSdkVersion);
        }
    }

    private void showNotSupportDialog(int version) {
        new AlertDialog.Builder(this)
                .setMessage(" The APP The target run version of is  " + version + ", Lower than the current version of the phone , Running... Is not supported !")
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        //  Exit procedure 
                        finish();
                    }
                })
                .create()
                .show();

    }
}

2、 stay AndroidManifest.xml Need to be defined in uses-sdk This label's targetSdkVersion attribute :

<uses-sdk android:targetSdkVersion="25" />

Be careful :

If in app In the catalog build.gradle It's defined in the file targetSdkVersion, It will cover AndroidManifest The value in , There are instructions in the source code :

file

3、 The renderings are as follows :

gif

We notice that the program uses getApplicationInfo().targetSdkVersion, So this variable is ApplicationInfo.java Member variables of , This value is in the installation APK In the process of 、 Served by the system PackageManagerService Analytic valuation , The source code is located in frameworks\base\core\java\android\content\pm\PackageParser.java in :

file

You can see :

  • First analyze TAG_USES_SDK(uses-sdk)

  • Then parse the property name targetSdkVersion as well as minSdkVersion Value

Finally, we assign it to applicationInfo Object's :

pkg.applicationInfo.targetSdkVersion = targetSdkVersion

This article is a platform of operation tools such as blog group sending one article and multiple sending OpenWrite Release

版权声明
本文为[snowyeti]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/02/20210201005155826w.html

Scroll to Top