我正在开发一个作为设备所有者运行的应用程序,我想在其中构建一个自动更新程序。
为此,我使用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 因此,接收器可以工作,但由于某种原因,它无法从包安装程序会话提交中工作。该应用程序关闭本身由于升级,但不会再次打开。
如果我将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();
}它实际上打开了电话服务。因此,我认为问题存在于升级生命周期中,因为当产生广播动作时,应用程序还没有准备好。
此外,我做了另一次尝试,我创建了一个边应用程序,它只会将广播操作调用到我的主应用程序,所以我可以调用这个侧应用程序,用这个“双重步骤”,它实际上可以重新打开刚刚更新的应用程序。问题是我必须安装两个应用程序=/
有人能帮我吗?有办法重新打开刚刚更新的应用程序吗?
发布于 2017-05-15 15:32:31
从Android5开始(我不知道确切的API级别),在你的应用程序更新后,安卓会用ACTION="android.intent.action.MY_PACKAGE_REPLACED"发送一个广播的"android.intent.action.MY_PACKAGE_REPLACED"。只需加上
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>对于您的<receiver>声明,您应该能够侦听这一点,并在onReceive()中重新启动应用程序。
https://stackoverflow.com/questions/43940990
复制相似问题