【问题标题】:Install apk from another program从另一个程序安装 apk
【发布时间】:2012-01-18 08:14:09
【问题描述】:

我正在尝试创建一个自动从特定服务器下载 apk 并将其安装在系统上的应用程序。我的安装代码如下所示,但不起作用。

File f = new File("/mnt/sdcard/download/", "Demo.apk");
Log.i("Demo", "f "+f.getAbsoluteFile());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(f), "application/vnd.android.package_archive");
intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
m_context.startActivity(intent);            

我需要在Manifest.xml 中授予任何安装权限吗?我知道以前有人问过这个问题,但到目前为止没有一个答案对我有帮助。

【问题讨论】:

  • 正如我在回答中提到的,使用“package-archive”而不是“package_archive”。希望这可以解决 ActivityNotFoundException。

标签: java android android-manifest apk


【解决方案1】:

如果您的targetSdkVersion 等于或大于24,那么您需要为Android N 和更新的Android 版本使用FileProvider 实现。

这是整个实现:

// utility method
private void openAppInstaller(Context context, File toInstall) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Uri apkUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", toInstall);
        Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
        intent.setData(apkUri);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(intent)
    } else {
        Uri apkUri = Uri.fromFile(toInstall);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
}

将提供商FileProvider 添加到AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    <application
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>
</manifest>

并在xml资源下创建provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external_files" path="."/>
</paths>

有关 FileProvider 配置的更多信息,您可以阅读 herehere

所有学分都归@just_user,因为我的回答基于his reply

【讨论】:

    【解决方案2】:

    感谢大家的帮助,终于成功了。我分享我的工作代码和工作 Manifest.xml。

    package test.installer;
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.Log;
    
    public class InstallToolActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("Demo", "onCreate");
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse("file://"+"/mnt/sdcard/HelloWorld.apk"), "application/vnd.android.package-archive");
        intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        }
    }
    

    Manifext.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="test.installer"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-sdk android:minSdkVersion="11" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
    <uses-permission android:name="android.permission.RESTART_PACKAGES"/>
    
    
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".InstallToolActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    

    //弗雷德里克

    【讨论】:

    • 您好 Fredrik,感谢您分享您在这方面的经验。我已按照完全相同的说明进行操作,但我收到“解析器错误。解析包时出现问题”。我尝试参考许多论坛,但没有太大帮助。任何形式的帮助表示赞赏。
    • 新手,这个问题有进展吗?
    • @Newbie 您是否对这两个软件使用相同的签名?你用的是哪个安卓版本?
    【解决方案3】:

    你需要这样做

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(f), "application/vnd.android.package-archive");
    

    在您的代码中,您提到了“package_archive”,它应该是“package-archive”。
    您需要以下权限。

    <uses-permission android:name="android.permission.INSTALL_PACKAGES"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    

    【讨论】:

      【解决方案4】:

      我也喜欢

      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      

      我的安装看起来像这样

      intent.setDataAndType(Uri.parse("file://"+path), "application/vnd.android.package-archive");
      

      我的path 是一个字符串,就像你的 f

      【讨论】:

        【解决方案5】:

        这就是我的情况,

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(path+"/<application_name>.apk")), "application/vnd.android.package-archive");
        startActivity(intent);
        

        这些是权限..

        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        

        【讨论】:

        • 还是不行,得到这个异常:E/AndroidRuntime(2216): java.lang.RuntimeException: Unable to start activity ComponentInfo{foo.bar/foo.bar.FooActivity}: android .content.ActivityNotFoundException:没有找到处理 Intent 的活动 { act=android.intent.action.VIEW dat=file:///mnt/sdcard/Demo.apk typ=application/vnd.android.package_archive flg=0x10000000 }我>
        • 现在不应该使用,因为它是用于系统应用程序的。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-17
        • 1970-01-01
        • 1970-01-01
        • 2012-05-27
        • 2011-09-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多