2014-03-26 183 views
1

我正試圖在用戶關閉應用程序安裝菜單時找到最佳解決方案。 如果用戶按下確定按鈕,應用程序安裝成功,意向PACKAGE_ADDED被髮送,但如何趕上CANCEL安裝按鈕?以編程方式安裝apk

我想到onStop,和onResume功能的一些標誌,但我認爲這是不正確的方式。

PS:另外,如果應用程序具有系統權限 PSS:我認爲不同的解決辦法像抽象的觀察者是不適合的。 我可以知道實現我的目標的正確方法是什麼?

回答

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