首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android 11软件包Visibilty

Android 11软件包Visibilty
EN

Stack Overflow用户
提问于 2020-05-30 13:43:36
回答 1查看 1.8K关注 0票数 1

我有一个从服务器下载apk的应用程序,但是我无法像其他android版本一样启动它,即使我在android清单中有查询标记。

这是意图代码:-

代码语言:javascript
复制
private void callInstallProcess() {
        try
        {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            File file = new File(pathname);
            Uri fileUri = Uri.fromFile(file);
//            if (Build.VERSION.SDK_INT >= 24) {
//                fileUri = FileProvider.getUriForFile(this, getPackageName(),
//                        file);
//            }
            if(Build.VERSION.SDK_INT>=24){
                try{
                    Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
                    m.invoke(null);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            intent.setDataAndType(fileUri , "application/vnd.android.package-archive");
            startActivityForResult(intent, PERMISSIONS_REQUEST);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

这是我的清单文件

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="x.x.x">
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:mimeType="application/vnd.android.package-archive" />
    </intent>
</queries>
    <application
        android:allowBackup="false"
        android:icon="@mipmap/appicon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/appicon"
        android:networkSecurityConfig="@xml/network_security_config"

        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

分级档案:-

代码语言:javascript
复制
apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "30.0.0-rc4"

    defaultConfig {
        applicationId "x.x.x"
        minSdkVersion 22
        targetSdkVersion 'R'
        versionCode 3
        versionName "1.11"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

我允许我的应用程序在android预览4模拟器上安装未知的应用程序。

日志:-

代码语言:javascript
复制
W/System.err: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///storage/emulated/0/Android/data/x.x.x/files/ApkDownload/x.x.x.apk typ=application/vnd.android.package-archive flg=0x3 }
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2067)
W/System.err:     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1727)
        at android.app.Activity.startActivityForResult(Activity.java:5323)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
        at android.app.Activity.startActivityForResult(Activity.java:5281)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
        at x.x.x.MainActivity.callInstallProcess(MainActivity.java:126)
        at x.x.x.MainActivity.askPerm(MainActivity.java:98)
W/System.err:     at x.x.x.MainActivity.access$000(MainActivity.java:42)
        at x.x.x.MainActivity$1.onClick(MainActivity.java:79)
        at androidx.appcompat.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7478)
        at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
D/EGL_emulation: eglMakeCurrent: 0xe8ae0220: ver 2 0 (tinfo 0xe8e31210)

我使用的是android 4,梯度是4.0.0,只是一个bug,还是我做错了什么,因为chrome设法想要进行apk安装。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-31 11:05:47

这只是个bug还是我做错了什么

你做错了什么。

实际上,从Android7.0开始,Uri.fromFile()就被禁止了。通常情况下,您使用Intent对我们的尝试会与FileUriExposedException崩溃。与其解决问题,不如通过disableDeathOnFileUriExposure攻击,尝试绕过检查。然而,没有人需要尊重你的file:/// Uri,因为这已经被禁止了很多年了。显然,Android的安装程序不再支持file:///

因此,至少,您需要切换到在Android 7.0+上使用FileProviderUri.fromFile()适用于Android6.0和更高版本,特别是对于您想要做的事情(安装APK),您别无选择,只能在这些旧设备上使用Uri.fromFile()

还要注意的是,使用ACTION_VIEW安装APK已经在Android 10+上被废弃了。它暂时仍然工作,但谷歌希望你切换到PackageInstaller API代替。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62103310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档