【问题标题】:Install apk from external storage from another application without confirmation activity从另一个应用程序的外部存储安装 apk,无需确认活动
【发布时间】:2015-03-12 14:48:35
【问题描述】:

这是通过确认活动从外部存储安装 apk 文件的代码示例:

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/app-debug.apk")), "application/vnd.android.package-archive");
        startActivity(intent);

我需要在没有确认活动的情况下从外部存储安装 apk。 有可能吗?

【问题讨论】:

  • 如果您的设备已“root”,这是可能的,那么我会给您代码。如果没有,那就不可能了。
  • 没有根是不可能的。这就是亚马逊和 Mikandi “应用商店”要求用户确认的原因。
  • 这是我为这个设备定制安卓的特定设备。
  • 我可以使用ndk、sdk或最低级别

标签: java android android-intent android-activity apk


【解决方案1】:

在有根设备上,您可以使用此代码,该代码也可以恢复为在无根设备上要求确认。这会自动更新实际运行的应用程序并重新启动它。

// path is the complete path to the APK file, startActivityName is the name of the activity to start once the install is complete.
private boolean rootUpdateAPK(String path, String startActivityName)
{       
    final String libs = "LD_LIBRARY_PATH=/vendor/lib:/system/lib ";
    final String[] commands = {
            libs + "pm install -r " + path,
            //              libs + "reboot"
            //      };
            libs + "am start -n " +  this.getPackageName() + "/."+startActivityName
    };
    execute_as_root(commands);  // not supposed to return if successful

    // device is not rooted, let's do it the "regular" way.

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

    return false;
}

private void execute_as_root( String[] commands ) {
    try {
        Process p = Runtime.getRuntime().exec( "su" );
        InputStream es = p.getErrorStream();
        DataOutputStream os = new DataOutputStream(p.getOutputStream());

        for( String command : commands ) {
            TVLog.i(TAG,"Execute as root: "+command);
            os.writeBytes(command + "\n");
        }
        os.writeBytes("exit\n");
        os.flush();
        os.close();

        int read;
        byte[] buffer = new byte[4096];
        String output = new String();
        while ((read = es.read(buffer)) > 0) {
            output += new String(buffer, 0, read);
        }

        p.waitFor();
        TVLog.i(TAG, output.trim() + " (" + p.exitValue() + ")");
    } catch (IOException e) {
        TVLog.i(TAG, e.getMessage());
    } catch (InterruptedException e) {
        TVLog.i(TAG, e.getMessage());
    }
}

【讨论】:

  • 它对我不起作用。我需要在没有确认和 root 的情况下从 apk 安装应用程序。
  • 你不能,或者你必须让你的应用程序成为系统应用程序并使其成为固件的一部分,因为你不想或不能root。
  • 我的应用是系统应用
【解决方案2】:

我解决了问题。 为了从任何存储安装 apk,我们可以使用 PackageManager 和隐藏的 api。 这是代码示例:

    PackageManager manager = getPackageManager();
    Uri packageURI = Uri.fromFile(new File(pathToApkFile));
    manager.installPackage(packageURI, null, 2, "app");

installPackage 它是隐藏的 api,你需要在 aosp 中构建你的应用程序

【讨论】:

    猜你喜欢
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多