安装完成后强制 android 应用程序打开

Force android app to open when it's installation complete

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

然后,为了实现对应用程序的更新,我查看是否有新的,如果有,我 运行 后台任务使用 [=13 从 sftp 服务器下载新的更新=],当应用程序完成下载时,我使用 ACTION_VIEW intent:

安装 apk
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);

这里的问题是安装完成后,出现默认的 android 屏幕:

这让用户可以选择完成 || 打开,并且确定如果用户选择完成将导致关闭window并且应用程序现在已经关闭,因为它已更新,因此它将用户带到根本不受欢迎的系统 UI。

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

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

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

我建议 install_referrer (reference)

在这个 中,我认为在 onReceive 中你可以执行任何你想要的代码,比如启动你的 Activity.

我从未使用或测试过这种方法,但它应该是可行的。

我终于解决了这个问题,我在一个端应用程序中使用了以下接收器:

    <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>

并且此应用始终是 运行,它只包含触发后的接收器(当原始应用更新时),我使用包名称启动原始应用:

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

以及为什么我确实使用另一个副应用程序作为监听器,那是因为BroadcastReceiver will never work unless the app is launched at least once,它在更新后不能直接通过原始应用程序使用。