当用户取消程序(".apk")安装时,如何检测情况? [英] How to detect the situation when a user cancels a program (".apk") installation?

查看:137
本文介绍了当用户取消程序(".apk")安装时,如何检测情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

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...操作.

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

如果用户选择不安装",则没有Intent.ACTION_PACKAGE...动作即可退出活动.

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

这意味着用户已取消安装".

That means "User has canceled an installation".

这篇关于当用户取消程序(".apk")安装时,如何检测情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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