如何让一个Service成为前置进程?
在启动该 Service 的时候可以在添加上如下方法:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification = new Notification(R.drawable.ic_launcher, "服务正
在运行",System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent,0);
notification.setLatestEventInfo(this,"消息标题","消息内容", pendingIntent);
startForeground(1, notification);
return super.onStartCommand(intent, flags, startId);
}
在启动该 Service 的时候可以在添加上如下方法:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification = new Notification(R.drawable.ic_launcher, "服务正
在运行",System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent,0);
notification.setLatestEventInfo(this,"消息标题","消息内容", pendingIntent);
startForeground(1, notification);
return super.onStartCommand(intent, flags, startId);
}