亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android應(yīng)用實(shí)現(xiàn)安裝后自啟動(dòng)的方法

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

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

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

ACTION_PACKAGE_REPLACED 一個(gè)新版本的應(yīng)用安裝到設(shè)備,替換之前已經(jīng)存在的版本

ACTION_PACKAGE_CHANGED 一個(gè)已存在的應(yīng)用程序包已經(jīng)改變,包括包名

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

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

ACTION_PACKAGE_DATA_CLEARED 用戶已經(jīng)清除一個(gè)包的數(shù)據(jù),包括包名(清除包程序不能接收到這個(gè)廣播)

直接思路:注冊(cè)廣播接收以上需要的action來實(shí)現(xiàn)。

但是,在安卓3.1之后,有了以下機(jī)制:

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)設(shè)置 - 應(yīng)用管理 中的“強(qiáng)制停止” 作用是讓app處于(stopped)停止?fàn)顟B(tài)。

下面是google的官方描述:

什么是停止?fàn)顟B(tài)?

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

注意:應(yīng)用程序的停止?fàn)顟B(tài)不同于activity(活動(dòng))的停止?fàn)顟B(tài)。系統(tǒng)是分開來處理這兩類停止?fàn)顟B(tài)的。

為什么Android要添加這個(gè)功能?

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

通過向廣播的Intent對(duì)象添加FLAG_INCLUDE_STOPPED_PACKAGES標(biāo)志,覆蓋重寫這個(gè)行為,使得該廣播可以激活處于停止?fàn)顟B(tài)的應(yīng)用程序。

上述描述指出:系統(tǒng)默認(rèn)會(huì)阻止停止?fàn)顟B(tài)的app接收廣播。這個(gè)控制機(jī)制的目的是保證安全、節(jié)約電量。

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

1、觸發(fā)ACTION_PACKAGE_REPLACED 廣播(也就是apk覆蓋替換安裝才接收的到,初次安裝的廣播ACTION_PACKAGE_ADDED 不會(huì)被當(dāng)前安裝包觸發(fā),因?yàn)樵揳pp未運(yùn)行過)

2、在app項(xiàng)目中使用靜態(tài)注冊(cè)廣播(因?yàn)閯?dòng)態(tài)廣播是app運(yùn)行后才可以接受到)

3、app曾經(jīng)運(yùn)行過(即不處于stopped狀態(tài))

在Android5.1真機(jī)上測(cè)試:

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

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

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

此時(shí)退出App,并在設(shè)置-應(yīng)用中把a(bǔ)pp進(jìn)行【強(qiáng)制停止】。覆蓋安裝后,app沒有自動(dòng)運(yùn)行。(此時(shí)在設(shè)置-應(yīng)用中查看,app處于stop狀態(tài))

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

下面簡(jiǎn)單介紹下代碼:

(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覆蓋安裝后自啟動(dòng)
		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)注冊(cè)廣播接收器

<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應(yīng)用實(shí)現(xiàn)安裝后自啟動(dòng)的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論