Android PackageInstaller,在更新后重新打开应用程序
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android PackageInstaller,在更新后重新打开应用程序,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3146字,纯文字阅读大概需要5分钟。
内容图文
![Android PackageInstaller,在更新后重新打开应用程序](/upload/InfoBanner/zyjiaocheng/822/3442be39600f40f4aa0e31a1d2156723.jpg)
我正在开发一个作为设备所有者运行的应用程序,我想在其中构建一个自动更新程序.
为此,我使用PackageInstaller,因为我拥有使用它的权限,因为我的设备所有者位置.
private void installPackage(InputStream inputStream)
throws IOException {
notifyLog("Inizio aggiornamento...");
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
int sessionId = packageInstaller.createSession(new PackageInstaller
.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL));
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
long sizeBytes = 0;
OutputStream out = null;
out = session.openWrite("my_app_session", 0, sizeBytes);
int total = 0;
byte[] buffer = new byte[65536];
int c;
while ((c = inputStream.read(buffer)) != -1) {
total += c;
out.write(buffer, 0, c);
}
session.fsync(out);
inputStream.close();
out.close();
session.commit(createIntentSender(sessionId));
}
private IntentSender createIntentSender(int sessionId) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context,
sessionId,
new Intent(LauncherReceiver.START_INTENT),
0);
return pendingIntent.getIntentSender();
}
更新是正确的,但问题是更新后它不会重新打开应用程序本身,即使我设置一个IntentSender将操作LauncherReceiver.START_INTENT广播到新的应用程序实例(这将启动它开始).
这是我的接收者:
public class LauncherReceiver extends BroadcastReceiver {
public static final String START_INTENT = "com.aaa.aaa.action.START";
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("CALL RECEIVER!!");
Intent startIntent = new Intent(context, StartActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(startIntent);
}
}
它已在我的清单中注册:
<receiver android:name=".receivers.LauncherReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="com.aaa.aaa.action.START"></action>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
如果我通过CLI调用它,它可以工作:
am broadcast -a com.worldnet.gestitruck.action.START
所以接收器工作但由于某种原因它不能从Package Installer会话提交工作.该应用由于升级而关闭,但不会再次打开.
如果我将createIntentSender更改为:
private IntentSender createIntentSender(int sessionId) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0123456789"));
PendingIntent pendingIntent = PendingIntent.getActivity(context,sessionId,intent,0);
return pendingIntent.getIntentSender();
}
它实际上打开了电话服务.所以我认为问题在于升级生命周期,因为在生成广播操作时应用程序还没有准备好.
此外,我再次尝试,我创建了一个侧面应用程序,除了将广播操作调用到我的主应用程序之外什么都不做,所以我可以调用这个侧面应用程序并通过这个“双步”它实际上可以重新打开刚刚更新的应用程序.问题是我必须安装两个应用程序= /
有谁能够帮我?有没有办法重新打开刚刚更新的应用程序?
解决方法:
从大约Android 5(我不知道确切的API级别)开始,Android会在您的应用更新后发送一个ACTION =“android.intent.action.MY_PACKAGE_REPLACED”的广播Intent.只需添加
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
到你的<接收器>声明,你应该能够听到这个并在onReceive()中重新启动你的应用程序.
内容总结
以上是互联网集市为您收集整理的Android PackageInstaller,在更新后重新打开应用程序全部内容,希望文章能够帮你解决Android PackageInstaller,在更新后重新打开应用程序所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。