Android-Service的?;罘椒?/h2>

?;頢ervice可從兩方面考慮:

一.改變Service自身的方法

1.提高Service的優(yōu)先級

在AndroidManifest.xml文件中對于intent-filter可以通過android:priority = "1000"這個屬性設(shè)置最高優(yōu)先級,1000是最高值,如果數(shù)字越小則優(yōu)先級越低,同時適用于廣播。

        <service
            android:name="com.dbjtech.acbxt.waiqin.UploadService"
            android:enabled="true" >
            <intent-filter android:priority="1000" >
                <action android:name="com.dbjtech.myservice" />
            </intent-filter>
        </service>

2.在Service即將銷毀的時候重新啟動

可以直接在onDestroy()里startService

    @Override
    public void onDestroy() {
 
         Intent sevice = new Intent(this, MainService.class);
         this.startService(sevice);
 
        super.onDestroy();
    }

也可以用service +broadcast 方式啟動:

onDestroy方法里重啟service,當(dāng)service走ondestory的時候,發(fā)送一個自定義的廣播,當(dāng)收到廣播的時候,重新啟動service;

        <receiver android:name="com.dbjtech.acbxt.waiqin.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="com.dbjtech.waiqin.destroy" />//這個就是自定義的action
            </intent-filter>
        </receiver>

在onDestroy時:

    @Override
    public void onDestroy() {
        stopForeground(true);
        Intent intent = new Intent("com.dbjtech.waiqin.destroy");
        sendBroadcast(intent);
        super.onDestroy();
    }

在BootReceiver里

public class BootReceiver extends BroadcastReceiver {
 
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("com.dbjtech.waiqin.destroy")) {
            //TODO
            //在這里寫重新啟動service的相關(guān)操作
                startUploadService(context);
        }
 
    }
 
}

3.onStartCommand方法,返回START_STICKY

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        flags = START_STICKY;
        return super.onStartCommand(intent, flags, startId);
    }

4.提升service進(jìn)程優(yōu)先級
在onStartCommand方法內(nèi)添加如下代碼:

         Notification notification = new Notification(R.drawable.ic_launcher,
         getString(R.string.app_name), System.currentTimeMillis());
        
         PendingIntent pendingintent = PendingIntent.getActivity(this, 0,
         new Intent(this, AppMain.class), 0);
         notification.setLatestEventInfo(this, "uploadservice", "請保持程序在后臺運行",
         pendingintent);
        startForeground(0x111, notification);

二.利用系統(tǒng)特性的方法

1.監(jiān)聽系統(tǒng)特殊事件

通過系統(tǒng)的一些廣播,比如:手機(jī)重啟、界面喚醒、應(yīng)用狀態(tài)改變等等監(jiān)聽并捕獲到,然后判斷我們的Service是否還存活,別忘記加權(quán)限啊。

        <receiver android:name="com.dbjtech.acbxt.waiqin.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="android.intent.action.PACKAGE_RESTARTED" />
                <action android:name="com.dbjtech.waiqin.destroy" />
            </intent-filter>
        </receiver>

BroadcastReceiver中:

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            System.out.println("手機(jī)開機(jī)了....");
            startUploadService(context);
        }
        if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) {
                startUploadService(context);
        }
    }

2.特殊手機(jī)監(jiān)聽特殊推送,例如小米手機(jī)注冊小米推送

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容