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

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

我的应用程序以编程方式安装其他应用程序,但用户可以取消安装过程.在安装过程中,用户会看到如下对话框:“替换应用程序.您正在安装的应用程序将替换另一个应用程序”.然后是一个对话框“你想安装这个应用程序吗?”.如果用户按下“安装”,则生成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();
        }
    }
}

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

如果用户选择“不安装”,则在没有Intent.ACTION_PACKAGE …操作的情况下退出活动.

这意味着“用户取消了安装”.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...