[英]What is the Difference between a Device Policy Controller (DCP) app and a System App?
[英]Upgrading app in background using Device Policy Controller
我有一個工作正常的DPC應用程序,它是設備所有者。 我已經在兩個不同的Android 6.0.1設備上進行了嘗試,以排除任何設備/制造商問題。
我使用了adb shell dpm set-device-owner com.example.dpc/.DpcDeviceAdminReceiver
來使我的DPC應用成為所有者。 成為所有者后,它可以正確地將COSU權限授予另一個應用程序,這使我確信這已經起作用。 該命令返回了響應:
Success: Device owner set to package com.example.dpc
Active admin set to component {com.examplem.dpc/com.example.dpc.DpcDeviceAdminReceiver}
我想使用此應用程序來安裝和升級另一個應用程序,而無需用戶干預(就像Google Play一樣)。
我正在使用以下概念驗證代碼:
void upgrade() {
String apkFileName = "app_debug_2_0_0";
PackageManager packageManger = getPackageManager();
PackageInstaller packageInstaller = packageManger.getPackageInstaller();
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL);
params.setAppPackageName("com.example.dummy");
try {
Log.e(TAG, "apkFileName " + apkFileName);
InputStream ins = getResources().openRawResource(
getResources().getIdentifier(apkFileName,
"raw", getPackageName()));
int sessionId = packageInstaller.createSession(params);
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
OutputStream out = session.openWrite(apkFileName, 0, -1);
final int bufsize = 4096;
byte[] bytes = new byte[bufsize];
int len = 1; // any value > 0
int tot = 0;
while (len > 0) {
len = ins.read(bytes, 0, bufsize);
Log.d(TAG, "len: " + len);
if (len < 1) break;
out.write(bytes, 0, len);
tot += len;
Log.d(TAG, "copied: " + tot);
}
ins.close();
session.fsync(out);
out.close();
Log.e(TAG, "about to commit ");
session.commit(PendingIntent.getBroadcast(this, sessionId,
new Intent("com.example.dpc.intent.UPDATE"), 0).getIntentSender());
Log.e(TAG, "committed ");
} catch (IOException e) {
Log.e(TAG, "Error installing package " + apkFileName, e);
}
}
使用上面的代碼和變體,我收到一個com.example.dpc.intent.UPDATE
意圖,其中包含錯誤:
Intent {
act=com.example.dpc.intent.UPDATE
flg=0x10
cmp=com.example.dpc/.DpcUpdateReceiver
bqHint=4
(has extras)
}
Bundle[
android.content.pm.extra.STATUS=4,
android.content.pm.extra.PACKAGE_NAME=com.example.dummy,
android.content.pm.extra.SESSION_ID=1055214117,
android.content.pm.extra.LEGACY_STATUS=-15,
android.content.pm.extra.STATUS_MESSAGE=INSTALL_FAILED_TEST_ONLY: installPackageLI
]
Logcat正確報告正在流式傳輸到session.openWrite
流中的apk
的大小。
我已經看過了:
apk
文件復制的字節數:1418604是正確的。 我究竟做錯了什么?
錯誤是來自frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
的以下Android 6.0.1代碼造成的。
if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_TEST_ONLY) != 0) {
if ((installFlags & PackageManager.INSTALL_ALLOW_TEST) == 0) {
res.setError(INSTALL_FAILED_TEST_ONLY, "installPackageLI");
return;
}
}
我檢查了要安裝的應用程序,發現已設置FLAG_TEST_ONLY
位。 為什么Android Studio 3.0.0在APK上設置FLAG_TEST_ONLY?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.