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

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

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到普洱網(wǎng)站設(shè)計(jì)與普洱網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類(lèi)型包括:網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)頁(yè)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋普洱地區(qū)。

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

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

直接思路:注冊(cè)廣播接收以上需要的action來(lái)實(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開(kāi)始,系統(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)是分開(kāi)來(lái)處理這兩類(lèi)停止?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)用程序可以

通過(guò)向廣播的Intent對(duì)象添加FLAG_INCLUDE_STOPPED_PACKAGES標(biāo)志,覆蓋重寫(xiě)這個(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)行過(guò))

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

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

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

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

覆蓋安裝未運(yùn)行過(guò)的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沒(méi)有自動(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è)參考,也希望大家多多支持創(chuàng)新互聯(lián)。

本文標(biāo)題:Android應(yīng)用實(shí)現(xiàn)安裝后自啟動(dòng)的方法
網(wǎng)頁(yè)URL:http://bm7419.com/article44/pcejee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站企業(yè)建站、品牌網(wǎng)站設(shè)計(jì)、企業(yè)網(wǎng)站制作做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開(kāi)發(fā)