以编程方式安装apk

时间:2014-03-26 13:38:36

标签: android

当用户关闭应用安装菜单时,我正在尝试找到最佳解决方案。 如果用户按下确定按钮并且应用程序安装成功,则发送了意图PACKAGE_ADDED但是如何捕获CANCEL安装按钮?

我考虑onStoponPauseonResume函数上的一些标记,但我认为这不是正确的方法。

PS:此外如果应用程序具有系统权限 PSS:我认为像抽象观察者这样的不同解决方法并不合适。 我可以知道实现目标的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

您可以监控当前的顶级Activity,并检查它是否为安装程序Activity。同时注册PACKAGE_ADDED等操作,以监控安装进度。 如果用户打开PackageInstallerActivity,然后返回ManageApplications活动,但您尚未收到PACKAGE_ADDED操作,那么您的应用程序未安装,那就是Cancel按钮操作。这就是你能做的一切。系统没有发送预安装操作。

class MonitorActivities extends Thread{

boolean exit = false;
ActivityManager am = null;
Context context = null;

public MonitorActivities (Context context){
    this.context = context;
    am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
}

public void run(){
    Looper.prepare();

    while(!exit){

        // Return a list of the tasks that are currently running,
        // with the most recent being first and older ones after in order.
        // Taken 1 inside getRunningTasks method means want to take only
        // top activity from stack and forgot the olders.
        List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);

        String activityName = taskInfo.get(0).topActivity.getClassName();

        Log.i("topActivity", "CURRENT Activity ::" + activityName);

        if(activityName.equals("com.android.packageinstaller.PackageInstallerActivity")) {
            // User is currently in application installation process

            exit = true;
        } else if(activityName.equals("com.android.settings.ManageApplications")) {
            // user has been taken back to Manage Applications window
            // we should close the activity monitoring now
            exit=true;
        }
    }
    Looper.loop();
}
}