安装完成后强制打开Android应用程序 [英] Force android app to open when it's installation complete

查看:98
本文介绍了安装完成后强制打开Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个 android 应用程序,它是系统上唯一运行的应用程序,因此,当启动完成时,我启动该应用程序并防止用户出于任何原因退出它(这使我禁用了两个导航状态栏).

然后,为了实现对应用程序的更新,我查看是否有新的更新,如果有,我运行后台任务使用

这让用户可以选择完成 ||打开,如果用户选择完成,这将导致关闭窗口,并且应用程序在更新时现在已经关闭,因此它会将用户带到完全不受欢迎的系统 UI.

请记住,我无法以编程方式打开应用程序,因为安装更新时它会卸载旧版本,因此我必须执行以下两个选项之一,但我不知道如何实现:

  1. 在安装完成后强制默认打开应用.

  2. 更改系统安装完成屏幕或覆盖其按钮操作.

解决方案

我终于解决了这个问题,我在一个side app中使用了以下接收器:

 <接收者android:name="com.example.extraApp.InstallReceiver"android:exported="true"><意图过滤器><action android:name="android.intent.action.PACKAGE_INSTALL"/><action android:name="android.intent.action.PACKAGE_ADDED"/><data android:scheme="package"/></意图过滤器></接收器>

而且这个应用程序一直在运行,它只包含那个接收器,在触发后(当原始应用程序更新时),我使用包名来启动原始应用程序:

public class InstallReceiver extends BroadcastReceiver{@覆盖public void onReceive(上下文上下文,意图意图){尝试 {Intent new_intent = context.getPackageManager().getLaunchIntentForPackage("com.example.updaterjschtest");context.startActivity(new_intent);} 捕获(异常 e){e.printStackTrace();}}}

<小时>

关于为什么我确实使用另一个辅助应用程序作为侦听器,那是因为 BroadcastReceiver 将永远不会工作,除非应用至少启动一次,这不适用于更新后直接通过原始应用.

I'm running an android app to be the only app to run on the system, so, when boot-completed I launch the app and prevent the user from exiting it for any reason(which made me disable both navigation status bars).

Then, to achieve an update to the app, I look if there is a new one, and if so, I run a background task to download the new update from the sftp server using jsch, and when the app finish downloading, I install the apk using ACTION_VIEW intent:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/update_app.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

The problem here is when the installation complete, the default android screen appears:

and this gives the user the option to choose Done || Open, and for sure if the user choose Done that will lead to close the window and the app is already closed now as it is updated, so it takes the user to the system UI which is not welcomed at all.

keep in mind that I can't open the app programmatically as when install the update it uninstall the old-version, so I've to do one of the two options which I don't know how to achieve any:

  1. Force the app to open by default after the installation finished.

  2. Change the system install finish screen or override it's buttons actions.

解决方案

I finally solved the problem, I used the following receiver in a side app:

    <receiver
        android:name="com.example.extraApp.InstallReceiver"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_INSTALL" />
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <data android:scheme="package"/>
        </intent-filter>
    </receiver>

and this app is always running and it contain nothing but that receiver, which is after triggered(when the original app updated), I use the package name to launch the original app:

public class InstallReceiver extends BroadcastReceiver{    
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Intent new_intent = context.getPackageManager().getLaunchIntentForPackage("com.example.updaterjschtest");
            context.startActivity(new_intent);
        } catch (Exception e) {
            e.printStackTrace();
        }   
    }
}


And about why I did use another side app as a listener, that's because the BroadcastReceiver will never work unless the app is launched at least once, which is not applicable through the original app directly after it got updated.

这篇关于安装完成后强制打开Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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