There is exception when install apk in android 10 by intent (android.content.ActivityNotFoundException)

0

I'm testing my android application to support android version 10.

When I test application in android 10 device, there is some problem with the apk install process.

Here is my source code and test information:-

        File realFilePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/app-debug.apk");
        if(realFilePath.exists()) {

            Uri apkUri = Uri.fromFile(realFilePath);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            try {
                startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

error message

2020-02-20 14:02:13.987 9572-9572/com.example.myapplication W/System.err: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///storage/emulated/0/app-debug.apk typ=application/vnd.android.package-archive flg=0x10000000 }
2020-02-20 14:02:13.987 9572-9572/com.example.myapplication W/System.err:     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2058)
2020-02-20 14:02:13.987 9572-9572/com.example.myapplication W/System.err:     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1716)
2020-02-20 14:02:13.987 9572-9572/com.example.myapplication W/System.err:     at android.app.Activity.startActivityForResult(Activity.java:5192)
2020-02-20 14:02:13.987 9572-9572/com.example.myapplication W/System.err:     at android.app.Activity.startActivityForResult(Activity.java:5150)
2020-02-20 14:02:13.987 9572-9572/com.example.myapplication W/System.err:     at android.app.Activity.startActivity(Activity.java:5521)
2020-02-20 14:02:13.987 9572-9572/com.example.myapplication W/System.err:     at android.app.Activity.startActivity(Activity.java:5489)

TEST information

================================
android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 15
        targetSdkVersion 22
================================

Android 9: there is no exception. Android 10: there is ActivityNotFoundException and fail to install apk.

I already know that I need to use a file provider to access an external file. But, as you can see that I set the targetSdkVersion version to 22.

targetSdkVersion 22

Why does the error only occur on an Android 10 device?

android
android-intent
android-sdk-tools
asked on Stack Overflow Feb 20, 2020 by Jeon • edited Feb 20, 2020 by Nikos Hidalgo

1 Answer

0

Try this:

if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
}

If there are no apps on the device that can receive the implicit intent, your app will crash when it calls startActivity(). To first verify that an app exists to receive the intent, call resolveActivity() on your Intent object. If the result is non-null, there is at least one app that can handle the intent and it's safe to call startActivity(). If the result is null, you should not use the intent.

answered on Stack Overflow Feb 20, 2020 by Meysam Safari

User contributions licensed under CC BY-SA 3.0