从另一个应用程序的外部存储安装 apk,无需确认 activity
Install apk from external storage from another application without confirmation activity
这是从外部存储安装 apk 文件并确认的代码示例 activity:
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 activity。
可能吗?
在获得 root 权限的设备上,您可以使用此代码,该代码也可以在非 root 设备上恢复询问确认。这会自动更新实际 运行 应用程序并重新启动它。
// 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 和 hidden api。
这是代码示例:
PackageManager manager = getPackageManager();
Uri packageURI = Uri.fromFile(new File(pathToApkFile));
manager.installPackage(packageURI, null, 2, "app");
installPackage 它是隐藏的 api 并且您需要在 aosp 中构建您的应用程序
这是从外部存储安装 apk 文件并确认的代码示例 activity:
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 activity。 可能吗?
在获得 root 权限的设备上,您可以使用此代码,该代码也可以在非 root 设备上恢复询问确认。这会自动更新实际 运行 应用程序并重新启动它。
// 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 和 hidden api。 这是代码示例:
PackageManager manager = getPackageManager();
Uri packageURI = Uri.fromFile(new File(pathToApkFile));
manager.installPackage(packageURI, null, 2, "app");
installPackage 它是隐藏的 api 并且您需要在 aosp 中构建您的应用程序