我要从服务器下载一个apk并以编程方式安装它。我有两种类型的设备:一种使用Android 6.0.1,另一种使用Android 4.2.2,两者均已扎根。在Android 6.0.1上它可以正常工作,但是在Android 4.2.2上,“ su”命令不起作用,因此我认为我应该使用系统认证(从以下位置收到的platform.pk8和platform.x509.pem)对apk进行签名制造商),我已经完成了。因此,问题是在Android 4.2.2上发现的,当未使用系统证书对apk进行签名时,会下载apk,但是当我对其进行签名并在清单中添加android:sharedUserId="android.uid.system
时,会收到下一个错误:
/mnt/sdcard/Download/aaa.apk: open failed: EACCESS(Permission denied).
我不知道该怎么办...如果apk未签名,则“ su”命令不起作用,如果已签名,我将收到权限被拒绝的错误:
/mnt/sdcard/Download/aaa.apk: open failed: EACCESS(Permission denied)
private void downloadApkFromURl(String url) {
Log.d("TAG", "" + "Started: " + Thread.currentThread().getName());
try {
URL url1 = new URL(url);
final HttpURLConnection c = (HttpURLConnection) url1.openConnection();
c.connect();
File sdcard = Environment.getExternalStorageDirectory();
File myDir = new File(sdcard, "/Download/");
myDir.mkdirs();
File outputFile = new File(myDir, "aaa.apk");
FileOutputStream fos = new FileOutputStream(outputFile);
final InputStream is = c.getInputStream();
byte[] buffer = new byte[4096];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.flush();
fos.close();
is.close();
final String command = "pm install -r " + outputFile.getAbsolutePath();
Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", command});
try {
proc.waitFor();
} catch (final InterruptedException e) {
sendErrorEvent(e.getMessage());
e.printStackTrace();
}
PackageManager pm = mContext.getPackageManager();
boolean isInstalled = isPackageInstalled("com.myPackage.app", pm);
if (isInstalled) {
deleteFileAfterInstall();
if (mListener != null) {
mListener.onSuccess();
}
Intent intent = mContext.getPackageManager().getLaunchIntentForPackage("com.myPackage.app");
mContext.startActivity(intent);
} else {
sendErrorEvent("App not installed");
}
} catch (final ProtocolException e) {
sendErrorEvent(e.getMessage());
e.printStackTrace();
} catch (final IOException e) {
sendErrorEvent(e.getMessage());
}
}
App Manifest文件:
manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myPackage.app"
**android:sharedUserId="android.uid.system**">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
<provider
android:name="com.myPackage.app.MultiprocessPreferences"
android:authorities="@string/multiprocess_preferences_authority"
android:exported="true"
/>
<service android:name=".ApkDownloaderService"
android:exported="true"
/>
</application>