是否可以通过静默方式自行更新设备所有者应用程序?我的应用设置为设备所有者,我希望它以静默方式进行自我更新。可能吗?
private void install(Context context,String packageName,String apkPath){
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
params.setAppPackageName(context.getPackageName());
try {
int sessionId = packageInstaller.createSession(params);
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
OutputStream out = session.openWrite(packageName,0,-1);
File apkFile = new File(apkPath);
InputStream in = new FileInputStream(apkFile);
byte[] buffer = new byte[65536];
int c;
while ((c = in.read(buffer)) != -1) {
out.write(buffer, 0, c);
}
session.fsync(out);
in.close();
out.close();
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,sessionId,
new Intent("com.afwsamples.testdpc.INSTALL_COMPLETE"),0);
IntentSender intentSender = pendingIntent.getIntentSender();
session.commit(intentSender);
}catch (Exception e){
Log.d("PackageInstaller","package installer exception");
}
}
我可以通过我的应用程序使用此方法安装另一个软件包,但不能通过我的应用程序本身安装。这是当我尝试安装作为设备所有者的同一应用程序时日志显示的内容。
--------- beginning of system
07-24 13:53:13.329 4039-4063/system_process I/PackageManager: init_copy
idx=0: InstallParams{a5de1df file=/data/app/vmdl1864928398.tmp cid=null}
07-24 13:53:13.338 4039-4063/system_process I/PackageManager: mcs_bound
startCopy UserHandle{0}: InstallParams{a5de1df
file=/data/app/vmdl1864928398.tmp cid=null}
07-24 13:53:13.341 3709-4027/? D/audio_hw_primary:
out_standby(0xf48e3000)
do_output_standby(0xf48e3000)
07-24 13:53:13.341 3709-4027/? I/audio_hw_primary: remove out
(0xf48e3000) from index 0
close pcm 0xf1b90380
07-24 13:53:13.347 4039-4063/system_process D/PackageManager:
/data/app/vmdl1864928398.tmp already staged; skipping copy
07-24 13:53:13.348 4039-4063/system_process D/PackageManager:
installPackageLI: path=/data/app/vmdl1864928398.tmpjava.lang.Throwable
at com.android.server.pm.PackageManagerService
.installPackageLI(PackageManagerService.java:12247)
at com.android.server.pm.PackageManagerService.-
wrap25(PackageManagerService.java)
at com.android.server.pm.PackageManagerService$9
.run(PackageManagerService.java:10176)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)
07-24 13:53:13.360 4039-4063/system_process W/PackageParser: Ignoring
duplicate uses-permissions/uses-permissions-sdk-m:
android.permission.INTERNET in package: com.xyz.cocoapp at: Binary
XML file line #23
Ignoring duplicate uses-permissions/uses-permissions-sdk-m:
android.permission.ACCESS_NETWORK_STATE in package: com.xyz.cocoapp
at: Binary XML file line #24
07-24 13:53:13.361 4039-4063/system_process W/PackageParser: Ignoring
duplicate uses-permissions/uses-permissions-sdk-m:
android.permission.WAKE_LOCK in package: com.xyz.cocoapp at: Binary
XML file line #30
Ignoring duplicate uses-permissions/uses-permissions-sdk-m:
android.permission.WAKE_LOCK in package: com.xyz.cocoapp at: Binary
XML file line #32
07-24 13:53:13.362 4039-4063/system_process W/PackageParser: Ignoring
duplicate uses-permissions/uses-permissions-sdk-m:
android.permission.GET_ACCOUNTS in package: com.xyz.cocoapp at:
Binary XML file line #33
07-24 13:53:13.363 4039-4063/system_process W/PackageParser: Ignoring
duplicate uses-permissions/uses-permissions-sdk-m:
android.permission.READ_EXTERNAL_STORAGE in package: com.xyz.cocoapp
at: Binary XML file line #45
07-24 13:53:13.369 4039-4063/system_process W/PackageManager:
installPackageLI
07-24 13:53:13.391 4039-4063/system_process I/art:
Starting a blocking GC Explicit
07-24 13:53:13.469 4039-4063/system_process I/art:
Explicit concurrent mark sweep GC freed 58637(2MB) AllocSpace objects,
10(340KB) LOS objects,
33% free, 10MB/15MB, paused 3.604ms total 77.999ms
07-24 13:53:13.470 3710-3710/?
E/installd: Couldn't opendir/data/app/vmdl1864928398.tmp: No such file
or directory
根据this stackoverflow question,可以对设备所有者应用进行自我更新。请帮助。