如何检测用户取消程序(“.apk”)安装时的情况?

发布于 2024-11-19 08:39:48 字数 216 浏览 2 评论 0原文

如何检测用户取消应用程序(.apk 文件)安装时的情况?

我的应用程序以编程方式安装其他应用程序,但用户可以取消安装过程。在安装过程中,用户会看到如下对话框:“替换应用程序。您正在安装的应用程序将替换另一个应用程序”。然后出现一个对话框“您想安装此应用程序吗?”。如果用户按“安装”,则会生成 ACTION_PACKAGE_... 广播。但是如何检测用户是否按下了“取消”呢?

How to detect the situation when a user cancels an application (.apk file) installation?

My app programmatically installs other apps, but users can cancel installation process. During the installation a user sees a dialog like that: "Replace application. The application you are installing will replace another application". And then a dialog "Do you want to install this application?". If the user presses "Install" the ACTION_PACKAGE_... broadcast is generated. But how to detect if the user presses "Cancel"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠我空喜 2024-11-26 08:39:48
public class ApplicationInstaller extends Activity {

    private final static int createState = 1, installState = 2;
    private int activityState = 0, counter = 0;
    private ApplicationInstallerReceiver aIR;
    private String appName, appPath;
    private boolean result;

    //---------------------------------------------------------------------------------------------
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_installer);
        if (activityState == 0) {
            this.activityState = ApplicationInstaller.createState;
            this.appName = this.getIntent().getStringExtra("AppName");
            this.appPath = this.getIntent().getStringExtra("AppPath");
            if (this.appName == null || this.appPath == null) {
                finish();
            }
        }
    }

    //---------------------------------------------------------------------------------------------
    @Override
    public void onStart() {
        super.onStart();
        if (this.activityState == ApplicationInstaller.createState) {
            activityState = ApplicationInstaller.installState;
            aIR = new ApplicationInstallerReceiver();
            IntentFilter ifilter = new IntentFilter();
            ifilter.addAction(Intent.ACTION_PACKAGE_ADDED);
            ifilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
            ifilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
            ifilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
            ifilter.addDataScheme("package");
            registerReceiver(aIR, ifilter);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(appPath)), "application/vnd.android.package-archive");
            try {
                startActivity(intent);
            } catch (Exception e) {
                result = false;
                finish();
            }
        } else {
            finish();
        }
    }

    //---------------------------------------------------------------------------------------------
    @Override
    protected void onResume() {
        super.onResume();
        counter++;
        if (counter == 2) {
            finish();
        }
    }

    //---------------------------------------------------------------------------------------------
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (aIR != null) {
            this.unregisterReceiver(aIR);
        }
        if (activityState == installState) {
            Intent intent = new Intent(DeviceSoftWareManager.installerAction);
            intent.putExtra("Result", this.result);
            sendBroadcast(intent);
        }
    }

    //---------------------------------------------------------------------------------------------
    class ApplicationInstallerReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            result = true;
            ApplicationInstaller.this.finish();
        }
    }
}

如果用户选择“安装”,将生成 Intent.ACTION_PACKAGE... 操作。

如果用户选择“不安装”,则 Activity 将退出,且不会执行 Intent.ACTION_PACKAGE... 操作。

这意味着“用户已取消安装”。

public class ApplicationInstaller extends Activity {

    private final static int createState = 1, installState = 2;
    private int activityState = 0, counter = 0;
    private ApplicationInstallerReceiver aIR;
    private String appName, appPath;
    private boolean result;

    //---------------------------------------------------------------------------------------------
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_installer);
        if (activityState == 0) {
            this.activityState = ApplicationInstaller.createState;
            this.appName = this.getIntent().getStringExtra("AppName");
            this.appPath = this.getIntent().getStringExtra("AppPath");
            if (this.appName == null || this.appPath == null) {
                finish();
            }
        }
    }

    //---------------------------------------------------------------------------------------------
    @Override
    public void onStart() {
        super.onStart();
        if (this.activityState == ApplicationInstaller.createState) {
            activityState = ApplicationInstaller.installState;
            aIR = new ApplicationInstallerReceiver();
            IntentFilter ifilter = new IntentFilter();
            ifilter.addAction(Intent.ACTION_PACKAGE_ADDED);
            ifilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
            ifilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
            ifilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
            ifilter.addDataScheme("package");
            registerReceiver(aIR, ifilter);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(appPath)), "application/vnd.android.package-archive");
            try {
                startActivity(intent);
            } catch (Exception e) {
                result = false;
                finish();
            }
        } else {
            finish();
        }
    }

    //---------------------------------------------------------------------------------------------
    @Override
    protected void onResume() {
        super.onResume();
        counter++;
        if (counter == 2) {
            finish();
        }
    }

    //---------------------------------------------------------------------------------------------
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (aIR != null) {
            this.unregisterReceiver(aIR);
        }
        if (activityState == installState) {
            Intent intent = new Intent(DeviceSoftWareManager.installerAction);
            intent.putExtra("Result", this.result);
            sendBroadcast(intent);
        }
    }

    //---------------------------------------------------------------------------------------------
    class ApplicationInstallerReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            result = true;
            ApplicationInstaller.this.finish();
        }
    }
}

If a user chooses "Install" Intent.ACTION_PACKAGE... actions will be generated.

If a user chooses "Don't install" an activity quits without Intent.ACTION_PACKAGE... actions.

That means "User has canceled an installation".

零時差 2024-11-26 08:39:48

我认为您无法检测用户是否卸载或未安装应用程序,但您可以在启动应用程序时或执行操作后检查是否安装了所需的其他应用程序

I think that you can't detect if the user uninstall or not install an app but you can check that the other apps required are installed when you start your app or after to do an action

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文