AlarmManager守护服务和隐藏桌面图标
1、主要内容
本章记录几段常用代码: 1、如何使用AlarmManager守护服务
2、如何判断某服务是否正在运行 2、如何暂时禁用Android的组件
2、使用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.java
@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); } } }
2、如何判断某服务是否正在运行
//判断服务是否在运行 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; }
3、如何暂时禁用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上点运行,替换原来的应用或者重启手机,系统仍然是禁用的(卸载后再安装系统会删除状态记录).
所以各位使用的时候需要注意到这一点,禁用后记得打开。
PS: 如果禁止的是主MainActivity,则桌面图标会消失