Android-Service的保活方法

支持原文:http://tryenough.com/android-service-life

保活Service可從兩方面考慮:

一.改變Service自身的方法

1.提高Service的優先級

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

        <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即將銷燬的時候重新啓動

支持原文:http://tryenough.com/android-service-life

可以直接在onDestroy()裏startService

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

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

onDestroy方法裏重啓service,當service走ondestory的時候,發送一個自定義的廣播,當收到廣播的時候,重新啓動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裏

支持原文:http://tryenough.com/android-service-life

public class BootReceiver extends BroadcastReceiver {
 
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("com.dbjtech.waiqin.destroy")) {
            //TODO
            //在這裏寫重新啓動service的相關操作
                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進程優先級
在onStartCommand方法內添加如下代碼:

         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);

二.利用系統特性的方法

支持原文:http://tryenough.com/android-service-life

1.監聽系統特殊事件

通過系統的一些廣播,比如:手機重啓、界面喚醒、應用狀態改變等等監聽並捕獲到,然後判斷我們的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="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("手機開機了....");
            startUploadService(context);
        }
        if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) {
                startUploadService(context);
        }
    }

2.特殊手機監聽特殊推送,例如小米手機註冊小米推送

支持原文:http://tryenough.com/android-service-life

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章