亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Android開發筆記——自我維護聲明周期的Service(避免系統殺死)

標簽:
Android

1.启动Service代码,可以放在点击事件中

Intent i = new Intent(this, XXXService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    startForegroundService(i);
} else {
    startService(i);
}

2.Service关键代码

public class SocketAlarmService extends Service {
    private static final long   CHECK_SERVICE_ALIVE_PERIOD                = HEARTBEAT * 2;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        startForeground();
    }

    void startForeground() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            MLog.i(FaceDetectionApp.TAG, "startForeground **************");
            NotificationChannel channel = new NotificationChannel(XXXID, "xxx", NotificationManager.IMPORTANCE_LOW);
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (manager != null) {
                manager.createNotificationChannel(channel);
            }
            Notification notification = new Notification.Builder(this, FACE_DETECTION_FOREGROUND_NOTIFICATION_ID).build();
            startForeground(-1, notification);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        MLog.i(FaceDetectionApp.TAG, "onStartCommand");
        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent i = new Intent(this, SocketKeepAliveReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
        if (someCondition/*不再维护声明周期,结束*/)) {
            stopSelf();// After stopSelf(), onDestroy will be executed
            return START_NOT_STICKY;
        }
        long triggerAtTime = SystemClock.elapsedRealtime() + CHECK_SERVICE_ALIVE_PERIOD;
        if (manager != null) {
            manager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
        }
        return super.onStartCommand(intent, flags, startId);
    }
}

3.自定义广播接收,用于启动Service,如果Service在运行,则只执行onStartCommon

public class XXXReceiver extends BroadcastReceiver {

    @Override

    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, XXXService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(i);
        } else {
            context.startService(i);
        }
    }
}

4.Manifest.xml

<service android:name=".poialert.XXXService"/>

<receiver
    android:name=".poialert.XXXReceiver"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
    <intent-filter>
        <action android:name="RestartSerivcesForSystemEventReceiver"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_MOUNTED"/>
        <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
        <action android:name="android.intent.action.MEDIA_EJECT"/>
        <data android:scheme="file"/>
    </intent-filter>
</receiver>

原文链接:http://www.apkbus.com/blog-184446-78091.html

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消