简体   繁体   English

以编程方式安装apk

[英]Installing apk programmatically

I'm trying to find best solution for catch moment when user closed app installation menu. 我正在尝试为用户关闭应用程序安装菜单时的时机找到最佳解决方案。 If user press OK button and app was installed success, the intent PACKAGE_ADDED was sent but how to catch CANCEL installation button? 如果用户按下“确定”按钮并且成功安装了应用程序,则发送了意图PACKAGE_ADDED,但是如何捕获“ CANCEL安装按钮?

I think about some flags on onStop , onPause and onResume functions but I think it is not right way. 我考虑了onStoponPauseonResume函数上的一些标志,但是我认为这不是正确的方法。

PS : Also If application has system permission PSS: I think different workaround like abstract observer is not suitable. PS:同样如果应用程序具有系统许可PSS:我认为像抽象观察器这样的不同解决方法是不合适的。 May I know what is the correct way to achieve my objective? 我可以知道实现目标的正确方法是什么?

You can monitor current top Activity , and check if it is installer Activity . 您可以监视当前的顶级Activity ,并检查它是否是安装程序Activity Also register for actions like PACKAGE_ADDED , for monitoring installation progress. 还注册诸如PACKAGE_ADDED ,以监视安装进度。 If user opened PackageInstallerActivity , then returned to ManageApplications activity, and you haven't received PACKAGE_ADDED action - then your application wasn't installed and that's the Cancel button action. 如果用户打开PackageInstallerActivity ,然后返回到ManageApplications活动,而您尚未收到PACKAGE_ADDED操作-则未安装您的应用程序,这就是“ Cancel按钮操作。 That's all what you can do. 这就是您所能做的。 There is no pre-install action sent by the system. 系统没有发送任何预安装操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM