Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从其他应用程序的外部存储安装apk,无需确认活动_Java_Android_Android Intent_Android Activity_Apk - Fatal编程技术网

Java 从其他应用程序的外部存储安装apk,无需确认活动

Java 从其他应用程序的外部存储安装apk,无需确认活动,java,android,android-intent,android-activity,apk,Java,Android,Android Intent,Android Activity,Apk,这是通过确认活动从外部存储器安装apk文件的代码示例: Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/app-debug.apk")), "application/vnd.android.package-archive");

这是通过确认活动从外部存储器安装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。
有可能吗?

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

// 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());
    }
}
我解决了这个问题。 为了从任何存储安装apk,我们可以使用PackageManager和隐藏api。 以下是代码示例:

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

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

如果你的设备是“根目录”的,这是可能的,那么我会给你代码。如果不是,则不可能。没有根是不可能的。这就是为什么亚马逊和Mikandi“应用商店”要求用户确认的原因。这是我为该设备定制android的特定设备。我可以使用ndk、sdk或最低级别它对我不起作用。我需要在没有确认和root的情况下从apk安装应用程序。你不能,或者你必须使你的应用程序成为系统应用程序,并使其成为固件的一部分,因为你不想或可以root。我的应用程序是系统应用程序