Force android app to open when it's installation complete

只谈情不闲聊 提交于 2021-01-28 00:23:35

问题


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.


回答1:


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.




回答2:


I would suggest the install_referrer (reference)

As in this answer, I think in onReceive you can execute every code you want, like starting your Activity.

I never used or tested this approach, but it should be doable.



来源:https://stackoverflow.com/questions/34706052/force-android-app-to-open-when-its-installation-complete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!