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

Android 開機(jī)自啟動Service實(shí)現(xiàn)詳解

 更新時(shí)間:2023年06月12日 09:39:42   作者:vivian310  
這篇文章主要為大家介紹了Android 開機(jī)自啟動Service實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

1、修改AndroidManifest.xml文件

// 添加接收開機(jī)廣播的權(quán)限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
// 注冊接收開機(jī)廣播的receiver
<receiver android:name=".BootBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <category android:name="android.intent.category.LAUNCHER"/>
     </intent-filter>
 </receiver>
//注冊需要啟動的Service
<service
    android:name=".TestService"
    android:exported="true"
    android:process="com.test.service">
    <intent-filter>
        <action android:name="com.test.Service" />
    </intent-filter>
</service>

2、recerver中啟動service

public class BootBroadcastReceiver extends BroadcastReceiver {
    static final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION_BOOT)){
            Intent mintent = new Intent(context, TestService.class);
            context.startService(mintent);
        }   
    }
}

3、去掉該服務(wù)APP的桌面圖標(biāo)

正常APP安裝后,在Launcher中會顯示圖標(biāo),由于我們的應(yīng)用是個后臺服務(wù),所以不需要顯示圖標(biāo),不顯示桌面圖標(biāo)有兩種方式

第一種

去掉Manifest文件中的<category android:name="android.intent.category.LAUNCHER" />該屬性

<activity
    android:name=".MainActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
</activity>

 備注這種做法在調(diào)試時(shí),不能通過編輯器直接運(yùn)行,需要編譯成APK,再手動安裝到設(shè)備中。

第二種

在activity的<intent-filter>標(biāo)簽中添加<data android:scheme="com.****.****"/>

<activity
    android:name=".MainActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        // "com.****.****"為應(yīng)用包名
        <data android:scheme="com.****.****"/>
    </intent-filter>
</activity>

 備注這種做法在調(diào)試時(shí),可以直接在編輯器中運(yùn)行,相對方便一些,兩種方式均可以隱藏桌面圖標(biāo)。

4、將APP放到/system/app目錄下

在Android3.1之后,系統(tǒng)為了加強(qiáng)安全性控制,應(yīng)用程序安裝后或是(設(shè)置)應(yīng)用管理中被強(qiáng)制關(guān)閉后處于stopped狀態(tài),在這種狀態(tài)下接收不到任何廣播。對于android3.1以后版本,如果要應(yīng)用接收開機(jī)廣播有兩種方法:

a).將應(yīng)用預(yù)置到/system/app/目錄。

b).安裝應(yīng)用后先啟動一次。(應(yīng)用只要啟動過一次,就不處于stopped狀態(tài))

以上就是Android 開機(jī)自啟動Service實(shí)現(xiàn)詳解的詳細(xì)內(nèi)容,更多關(guān)于Android 開機(jī)自啟動Service的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論