和網(wǎng)上大多數(shù)方法一樣,使用廣播手段:
ACTION_PACKAGE_ADDED 一個新應用包已經(jīng)安裝在設備上,數(shù)據(jù)包括包名(最新安裝的包程序不能接收到這個廣播)
ACTION_PACKAGE_REPLACED 一個新版本的應用安裝到設備,替換之前已經(jīng)存在的版本
ACTION_PACKAGE_CHANGED 一個已存在的應用程序包已經(jīng)改變,包括包名
ACTION_PACKAGE_REMOVED 一個已存在的應用程序包已經(jīng)從設備上移除,包括包名(正在被安裝的包程序不能接收到這個廣播)
ACTION_PACKAGE_RESTARTED 用戶重新開始一個包,包的所有進程將被殺死,所有與其聯(lián)系的運行時間狀態(tài)應該被移除,包括包名(重新開始包程序不能接收到這個廣播)
ACTION_PACKAGE_DATA_CLEARED 用戶已經(jīng)清除一個包的數(shù)據(jù),包括包名(清除包程序不能接收到這個廣播)
直接思路:注冊廣播接收以上需要的action來實現(xiàn)。
但是,在安卓3.1之后,有了以下機制:
force-stop in Manage Application of Settings makes App in a stopped state!
Here is what Google describes
What is Stopped State
Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state and provides a means of controlling their launch from background processes and other applications. Note that an application's stopped state is not the same as an Activity's stopped state. The system manages those two stopped states separately.