1. 在onStartCommand方法中将flag设置为START_STICKY;

  1. return Service.START_STICKY;

2. 在xml中设置了android:priority

  1. <!--设置服务的优先级为MAX_VALUE-->
  2. <service android:name=".MyService"
  3. android:priority="2147483647"
  4. >
  5. </service>

3. 在onStartCommand方法中设置为前台进程

  1. @Override
  2. public int onStartCommand(Intent intent, int flags, int startId) {
  3. Notification notification = new Notification(R.mipmap.ic_launcher, "服务正在运行",System.currentTimeMillis());
  4. Intent notificationIntent = new Intent(this, MainActivity.class);
  5. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent,0);
  6. RemoteViews remoteView = new RemoteViews(this.getPackageName(),R.layout.notification);
  7. remoteView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
  8. remoteView.setTextViewText(R.id.text , "Hello,this message is in a custom expanded view");
  9. notification.contentView = remoteView;
  10. notification.contentIntent = pendingIntent;
  11. startForeground(1, notification);
  12. return Service.START_STICKY;
  13. }

4. 在onDestroy方法中重启service

  1. @Override
  2. public void onDestroy() {
  3. super.onDestroy();
  4. startService(new Intent(this, MyService.class));
  5. }

5. 用AlarmManager.setRepeating(…)方法循环发送闹钟广播,接收的时候调用service的onstart方法

  1. Intent intent = new Intent(MainActivity.this,MyAlarmReciver.class);
  2. PendingIntent sender = PendingIntent.getBroadcast( MainActivity.this, 0, intent, 0);
  3. // We want the alarm to go off 10 seconds from now.
  4. Calendar calendar = Calendar.getInstance();
  5. calendar.setTimeInMillis(System.currentTimeMillis());
  6. calendar.add(Calendar.SECOND, 1);
  7. AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
  8. //重复闹钟
  9. /**
  10. * @param type
  11. * @param triggerAtMillis t 闹钟的第一次执行时间,以毫秒为单位
  12. * go off, using the appropriate clock (depending on the alarm type).
  13. * @param intervalMillis 表示两次闹钟执行的间隔时间,也是以毫秒为单位
  14. * of the alarm.
  15. * @param operation 绑定了闹钟的执行动作,比如发送一个广播、给出提示等等
  16. */
  17. am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 2 * 1000, sender);

6. 目前市场面的很多三方的消息推送SDK唤醒APP,例如Jpush

img

总结

这纯粹是面试的时候忽悠一下面试官,不代表着你的Service就永生不死了,只能说是提高了进程的优先级。迄今为止我没有发现能够通过常规方法达到流氓需求(通过长按home键清除都清除不掉)的方法,目前所有方法都是指通过Android的内存回收机制和普通的第三方内存清除等手段后仍然保持运行的方法,有些手机厂商把这些知名的app放入了自己的白名单中,保证了进程不死来提高用户体验(如微信、QQ、陌陌都在小米的白名单中)。如果从白名单中移除,他们终究还是和普通app一样躲避不了被杀的命运。