AlarmManager守護服務和隱藏桌面圖標

一、主要內容

本章記錄幾段經常使用代碼:
一、如何使用AlarmManager守護服務
二、如何判斷某服務是否正在運行 二、如何暫時禁用Android的組件

 二、使用AlarmManager守護服務

BootReceiver.java
@Override
public void onReceive(Context context, Intent intent) {
    //接收開機啓動廣播,啓動服務,開啓alarm定時任務
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        //xxx.class咱們要守護的服務
        Intent serviceIntent = new Intent(context, xxxx.class);
        context.startService(serviceIntent);
        startAlarmEvent(context);
    }
    //監聽自定義廣播,開啓alarm定時任務
    if (intent.getAction().equals("xxxxx")) {
        startAlarmEvent(context);
    }

     //監聽自定義廣播,關閉alarm定時任務
    if (intent.getAction().equals("cn.zcgames.ghostclaw.action.STOP_SERVICE")) {
        Log.d(Tag, "Receive Stop Service Completed broadcast, start Alarm");
        stopAlarmEvent(context);
    }
}

//開啓定時任務,手動開啓的時候調用
private void startAlarmEvent(Context context) {
    Intent alarmIntent = new Intent(context, AlarmReceiver.class);
    //listen_receiver爲AlarmReceiver.java註冊的感興趣的廣播
    alarmIntent.setAction("listen_receiver");
    PendingIntent sender = PendingIntent.getBroadcast(context, 0,
            alarmIntent, 0);
    long firstime = SystemClock.elapsedRealtime();
    AlarmManager alarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    //每6秒鐘發送一次廣播,API 23後,最短期間隔爲6秒
    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime, 10 * 6000, sender);
}
//關閉定時任務,手動關閉的時候調用,防止本身也中止不了
private void stopAlarmEvent(Context context) {
    Intent alarmIntent = new Intent(context, AlarmReceiver.class);
    //listen_receiver爲AlarmReceiver.java註冊的感興趣的廣播
    alarmIntent.setAction("listen_receiver");
    PendingIntent sender = PendingIntent.getBroadcast(context, 0,
            alarmIntent, 0);
    AlarmManager alarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    //取消發送廣播
    alarmManager.cancel(sender);
}

AlarmReceiver.javajava

@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("listen_receiver")) {
        //若是監聽的服務掛掉了,則啓動服務
        if (serviceIsRunnting(context, "xxxService")) {
            Intent serviceIntent = new Intent(context, xxxService.class);
            context.startService(serviceIntent);
        }
    }
}

二、如何判斷某服務是否正在運行

//判斷服務是否在運行
boolean serviceIsRunnting(Context context,  String servcieName) {
    boolean isRunning = false;
    ActivityManager activityManager=null;
    List<ActivityManager.RunningServiceInfo> serviceList=null;

    try{
        activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    }catch (Exception e){
        e.printStackTrace();
    }
    if(activityManager != null){
        serviceList = activityManager.getRunningServices(Integer.MAX_VALUE);
    }

    if (!(serviceList.size()>0)) {
        return false;
    }

    for (int i=0; i<serviceList.size(); i++) {
        if (serviceList.get(i).service.getClassName().equals(servcieName)) {
            isRunning = true;
            break;
        }
    }
    return isRunning;
}

三、如何暫時禁用Android的組件

private void setComponentState(boolean enable){
    //xxx爲app的某組件,我這裏是receiver
    final ComponentName alarmReceiver = new ComponentName(getApplication(), xxxx.class);
    final PackageManager packageManager = getApplication().getPackageManager();

    if(enable){
        packageManager.setComponentEnabledSetting(alarmReceiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }else{
        packageManager.setComponentEnabledSetting(alarmReceiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }
}

 根據個人使用經驗,當在應用中禁用某組件後,系統會記錄此狀態,直接在as上點運行,替換原來的應用或者重啓手機,系統仍然是禁用的(卸載後再安裝系統會刪除狀態記錄).app

 因此各位使用的時候須要注意到這一點,禁用後記得打開。ide

 

PS: 若是禁止的是主MainActivity,則桌面圖標會消失spa

相關文章
相關標籤/搜索