Android PackageInstaller,在更新自身后重新打开该应用 [英] Android PackageInstaller, re-open the app after it updates itself

查看:336
本文介绍了Android PackageInstaller,在更新自身后重新打开该应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing an app that runs as Device Owner, and I want to build an automatic updater inside it.

为此,我使用PackageInstaller,因为由于我的设备所有者身份,我有使用它的特权.

To do it I use the PackageInstaller, as I have the privileges to use it due to my Device owner position.

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广播到新的应用程序实例中,它也不会在更新后重新打开应用程序本身(它将带来新的应用程序实例)上开始).

The update goes right, but the problem is that it doesn't re-open the app itself after the update, even if I set an IntentSender to broadcast the action LauncherReceiver.START_INTENT to the new app instance (that will bring it on to start).

这是我的接收者:

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);
    }
}

它已在我的清单中注册:

And it is registered in my manifest:

    <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调用它,它将起作用:

If I call it by CLI it works:

am broadcast -a com.worldnet.gestitruck.action.START 

因此,接收器可以工作,但是由于某些原因,它在Package Installer会话提交中不起作用.该应用会由于升级而自行关闭,但不会再次打开.

So the receiver works but for some reason it doesn't work from the Package Installer session commit. The app close itself due to the upgrade but does't open again.

如果我将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();
}

它实际上打开了电话服务.因此,我认为问题出在升级生命周期中,因为在生成广播操作时应用尚未准备就绪.

it actually opens the phone service. So I think the problem reside in the upgrade lifecycle, as the app is not ready when the broadcast action is spawned.

此外,我再次尝试,创建了一个侧面应用程序,该应用程序除了将广播操作调用到我的主应用程序外什么也不做,因此我可以调用该侧面应用程序,并通过双重步骤"实际上可以重新打开该应用程序.更新的应用程序.问题是我必须安装两个应用程序=/

Moreover, I made another try and I created a side app that does nothing but call the broadcast action to my main app, so I can call this side app and with this "double step" it can actually re-open the just updated app. The problem is that I have to install two apps =/

有人可以帮助我吗?有没有办法重新打开刚刚更新的应用程序?

Can anybody help me? Is there a way to re-open the just updated app?

推荐答案

从大约Android 5(我不知道确切的API级别)开始,Android会在您的设备之后以ACTION = "android.intent.action.MY_PACKAGE_REPLACED"发送广播Intent应用已更新.只需添加

Starting from about Android 5 (I don't know the exact API level), Android will send a broadcast Intent with ACTION="android.intent.action.MY_PACKAGE_REPLACED" after your app is updated. Just add

        <intent-filter>
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
        </intent-filter>

<receiver>声明中,您应该可以侦听此内容并在onReceive()中重新启动应用.

to your <receiver> declaration and you should be able to listen for this and restart your app in onReceive().

这篇关于Android PackageInstaller,在更新自身后重新打开该应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆