1.创建 MyBackgroundService.java 继承 Service
public class MyBackgroundService extends Service {@Overridepublic void onCreate() {super.onCreate();Log.i("业务服务", "开起业务服务");//调用服务后在页面手机上创建一个通知消息。if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);NotificationChannel channel = null;channel = new NotificationChannel("100", getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);notificationManager.createNotificationChannel(channel);Notification notification = new Notification.Builder(getApplicationContext(), "100").setVisibility(Notification.VISIBILITY_PRIVATE).build();startForeground(100, notification);}}@Overridepublic int onStartCommand(Intent intent,int flsgs,int startId){timer.schedule(task, 60000, 60000); //定时执行代码return START_STICKY;}@Overridepublic IBinder onBind(Intent intent) {return null;} }
//构造消息(在需要发送消息的方法中调用方法) private void sendMessgae(String my_channel_ID,String my_channel_NAME,String msgContext,int notifyid,String msgType){PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);String channelId = createNotificationChannel(my_channel_ID, my_channel_NAME, NotificationManager.IMPORTANCE_HIGH);NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), channelId).setContentTitle("管理系统").setContentText(msgContext).setContentIntent(pendingIntent).setSmallIcon(R.drawable.newlogo).setAutoCancel(true);NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());if(notification !=null){notificationManager.notify(notifyid,notification.build());savemsg(my_channel_ID+"",msgContext,msgType);} }//消息设置 private String createNotificationChannel(String channelID, String channelNAME, int level) {if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);//开启震动channel.enableVibration(true);//设置锁屏提示channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);manager.createNotificationChannel(channel);return channelID;} else {return null;} }
2.创建 DaemonService.java 守护服务
public class DaemonService extends Service {
private final static String TAG = DaemonService.class.getSimpleName();@Overridepublic void onCreate() {super.onCreate();Log.i("测试开启服务", "开始守护服务");}@Overridepublic int onStartCommand(Intent intent,int flags,int startId){if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {startForegroundService(new Intent(this, MyBackgroundService.class));} else {startService(new Intent(this, MyBackgroundService.class));}return START_STICKY;}
}
3.在 AndroidManifest.xml 中注册服务
<service android:name=".tools.DaemonService"android:process=":daemon" /><serviceandroid:name=".tools.MyBackgroundService"android:enabled="true"android:exported="false"android:stopWithTask="false" />
手动关闭应用程序后后台进程一直存在。