欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android應用實現(xiàn)安裝后自啟動的方法

 更新時間:2018年08月06日 10:51:37   作者:倆兜沒錢  
今天小編就為大家分享一篇Android應用實現(xiàn)安裝后自啟動的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

和網上大多數(shù)方法一樣,使用廣播手段:

ACTION_PACKAGE_ADDED 一個新應用包已經安裝在設備上,數(shù)據(jù)包括包名(最新安裝的包程序不能接收到這個廣播)

ACTION_PACKAGE_REPLACED 一個新版本的應用安裝到設備,替換之前已經存在的版本

ACTION_PACKAGE_CHANGED 一個已存在的應用程序包已經改變,包括包名

ACTION_PACKAGE_REMOVED 一個已存在的應用程序包已經從設備上移除,包括包名(正在被安裝的包程序不能接收到這個廣播)

ACTION_PACKAGE_RESTARTED 用戶重新開始一個包,包的所有進程將被殺死,所有與其聯(lián)系的運行時間狀態(tài)應該被移除,包括包名(重新開始包程序不能接收到這個廣播)

ACTION_PACKAGE_DATA_CLEARED 用戶已經清除一個包的數(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.

Why Android Adds this

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications. A background service or application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowed to activate stopped applications.

As the above references point out it will prevent broadcast intents delivering to stopped packages. Actually this control mechanism will ensure safety and save energy.

Android 3.1 APIs

翻譯:

在 系統(tǒng)設置 - 應用管理 中的“強制停止” 作用是讓app處于(stopped)停止狀態(tài)。

下面是google的官方描述:

什么是停止狀態(tài)?

從Andriod3.1開始,系統(tǒng)包管理服務會一直追蹤處于停滯狀態(tài)的app,并提供了控制它們從后臺進程或其他應用程序啟動的方法。

注意:應用程序的停止狀態(tài)不同于activity(活動)的停止狀態(tài)。系統(tǒng)是分開來處理這兩類停止狀態(tài)的。

為什么Android要添加這個功能?

注意:系統(tǒng)為所有用于發(fā)送廣播的Intent默認添加了FLAG_EXCLUDE_STOPPED標志。這樣做是為了阻止發(fā)送自后臺service的廣播不小心啟動某個已停止應用的組件。一個后臺service服務或app應用程序可以

通過向廣播的Intent對象添加FLAG_INCLUDE_STOPPED_PACKAGES標志,覆蓋重寫這個行為,使得該廣播可以激活處于停止狀態(tài)的應用程序。

上述描述指出:系統(tǒng)默認會阻止停止狀態(tài)的app接收廣播。這個控制機制的目的是保證安全、節(jié)約電量。

所以,要實現(xiàn)安裝apk后自啟動,前提是

1、觸發(fā)ACTION_PACKAGE_REPLACED 廣播(也就是apk覆蓋替換安裝才接收的到,初次安裝的廣播ACTION_PACKAGE_ADDED 不會被當前安裝包觸發(fā),因為該app未運行過)

2、在app項目中使用靜態(tài)注冊廣播(因為動態(tài)廣播是app運行后才可以接受到)

3、app曾經運行過(即不處于stopped狀態(tài))

在Android5.1真機上測試:

初次安裝的app不會觸發(fā)廣播。

覆蓋安裝未運行過的app,不會觸發(fā)廣播

安裝完運行app后,退出App(點擊返回鍵、并從recent任務中移除,此時在設置-應用中查看,app仍未處于stop狀態(tài))。覆蓋安裝后,app成功自動運行。(可看做實現(xiàn)安裝后自啟動)

此時退出App,并在設置-應用中把app進行【強制停止】。覆蓋安裝后,app沒有自動運行。(此時在設置-應用中查看,app處于stop狀態(tài))

所以,只要在App運行時,直接覆蓋安裝apk,是可以用廣播接收器實現(xiàn)安裝完后自啟動的。 (至少在android 5.1上 ^,^)

下面簡單介紹下代碼:

(1)自定義廣播接收器:

public class MyReceiver extends BroadcastReceiver {
 
	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		String localPkgName = context.getPackageName();//取得MyReceiver所在的App的包名
		Uri data = intent.getData();
		String installedPkgName = data.getSchemeSpecificPart();//取得安裝的Apk的包名,只在該app覆蓋安裝后自啟動
		if((action.equals(Intent.ACTION_PACKAGE_ADDED)
				|| action.equals(Intent.ACTION_PACKAGE_REPLACED)) && installedPkgName.equals(localPkgName)){
			Intent launchIntent = new Intent(context,MainActivity.class);
			launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(launchIntent);
		}
	}
}

(2)AndroidManifest.xml中靜態(tài)注冊廣播接收器

<application
  android:allowBackup="true"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/AppTheme" >
  <activity
   android:name="com.example.mydemo.MainActivity"
  	android:configChanges="orientation|keyboard"
   android:label="@string/app_name">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
  <receiver android:name="com.example.mydemo.MyReceiver">
   <intent-filter>
    <action android:name="android.intent.action.PACKAGE_ADDED" /> 
    <action android:name="android.intent.action.PACKAGE_REPLACED" />
    <action android:name="android.intent.action.PACKAGE_REMOVED" />
    <data android:scheme="package"/>
   </intent-filter>
  </receiver>
 </application>

以上這篇Android應用實現(xiàn)安裝后自啟動的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論