极光推送(守护进程)
16lz
2022-05-16
采用双服务的方式,有MainService(推送服务)和CoreService(守护服务),能够在一定程度上解决服务被杀的问题。
一. 将极光推送Service 和主进程隔离开
Required SDK 核心功能 可配置android:process参数将PushService放在其他进程中
android:process=":ServiceProcess" android:exported="false">
二. 新建一个轻量级的CoreService作为守护,主要用于唤醒进程
public class CoreService extends Service { private static final String TAG = CoreService.class.getSimpleName(); @Override public IBinder onBind(final Intent intent) { Log.d(TAG, "onBind()"); return null; } @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate()"); keepMeAlive(); } @Override public void onDestroy() { Log.d(TAG, "onDestroy()"); super.onDestroy(); } @Override public int onStartCommand(final Intent intent, final int flags, final int startId) { // send status notice// IntentHandler.sendStatusNoticeToAll(this); this.startService(new Intent(this,PushReceiver.class)); Log.d(TAG, "onStartCommand"); return START_STICKY; } public void keepMeAlive() { // check every two hour to start process final long now = System.currentTimeMillis(); final long intervalMillis = 1000*30; final long triggerAtMillis = now + intervalMillis; final Intent intent = new Intent(this, CoreService.class); final PendingIntent operation = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); final AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtMillis, intervalMillis, operation); }}
更多相关文章
- Android(安卓)Service
- android 系统资源的加载和获取
- Android(安卓)adb setuid提权漏洞的分析
- android binder机制之二 Service Manager
- AIDL笔记
- Android(安卓)P 以及之后版本不支持同时从多个进程使用具有相同
- Binder 简介
- Android(安卓)SDK代理服务器解决国内不能更新下载问题
- Android(安卓)OnLowMemory和OnTrimMemory