[英]Installing apk programmatically
我正在嘗試為用戶關閉應用程序安裝菜單時的時機找到最佳解決方案。 如果用戶按下“確定”按鈕並且成功安裝了應用程序,則發送了意圖PACKAGE_ADDED,但是如何捕獲“ CANCEL
安裝按鈕?
我考慮了onStop
, onPause
和onResume
函數上的一些標志,但是我認為這不是正確的方法。
PS:同樣如果應用程序具有系統許可PSS:我認為像抽象觀察器這樣的不同解決方法是不合適的。 我可以知道實現目標的正確方法是什么?
您可以監視當前的頂級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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.