首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android PackageInstaller,更新后重新打开应用程序。

Android PackageInstaller,更新后重新打开应用程序。
EN

Stack Overflow用户
提问于 2017-05-12 15:07:11
回答 1查看 5.9K关注 0票数 21

我正在开发一个作为设备所有者运行的应用程序,我想在其中构建一个自动更新程序。

为此,我使用PackageInstaller,因为由于设备所有者的位置,我拥有使用它的权限。

代码语言:javascript
复制
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广播到新的应用实例(这将使其启动)。

这是我的接收器:

代码语言:javascript
复制
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);
    }
}

它在我的舱单上登记:

代码语言:javascript
复制
    <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来称呼它,它会起作用:

代码语言:javascript
复制
am broadcast -a com.worldnet.gestitruck.action.START 

因此,接收器可以工作,但由于某种原因,它无法从包安装程序会话提交中工作。该应用程序关闭本身由于升级,但不会再次打开。

如果我将createIntentSender更改为:

代码语言:javascript
复制
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();
}

它实际上打开了电话服务。因此,我认为问题存在于升级生命周期中,因为当产生广播动作时,应用程序还没有准备好。

此外,我做了另一次尝试,我创建了一个边应用程序,它只会将广播操作调用到我的主应用程序,所以我可以调用这个侧应用程序,用这个“双重步骤”,它实际上可以重新打开刚刚更新的应用程序。问题是我必须安装两个应用程序=/

有人能帮我吗?有办法重新打开刚刚更新的应用程序吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-15 15:32:31

从Android5开始(我不知道确切的API级别),在你的应用程序更新后,安卓会用ACTION="android.intent.action.MY_PACKAGE_REPLACED"发送一个广播的"android.intent.action.MY_PACKAGE_REPLACED"。只需加上

代码语言:javascript
复制
        <intent-filter>
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
        </intent-filter>

对于您的<receiver>声明,您应该能够侦听这一点,并在onReceive()中重新启动应用程序。

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43940990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档