gpt4 book ai didi

android - 以编程方式安装 apk

转载 作者:行者123 更新时间:2023-11-30 03:00:01 24 4
gpt4 key购买 nike

我正在尝试为用户关闭应用程序安装菜单时捕捉瞬间找到最佳解决方案。如果用户按下 OK 按钮并且应用程序安装成功,则会发送 Intent PACKAGE_ADDED 但是如何捕获 CANCEL 安装按钮?

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

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

最佳答案

您可以监控当前的顶级Activity,并检查它是否是安装程序Activity。还可以注册 PACKAGE_ADDED 等操作,以监控安装进度。如果用户打开 PackageInstallerActivity,然后返回到 ManageApplications Activity ,并且您还没有收到 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();
}
}

关于android - 以编程方式安装 apk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22662743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com