清单配置:
<service
android:name="com.service.Service1"
android:enabled="true"
android:process=":Service1">
</service>
<service
android:name="com.service.Service2"
android:enabled="true"
android:process=":Service2">
</service>
代码片段:
public class Service1 extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification.Builder builder = new Notification.Builder(this);
builder.setContentInfo("补充内容");
builder.setContentText("正在运行...");
builder.setContentTitle("进行标题");
builder.setSmallIcon(R.mipmap.ic_laucher);
builder.setTicker("新消息");
builder.setAutoCancel(true);
builder.setWhen(System.currentTimeMillis());
Intent notificationIntent = new Intent(this, SplashActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
builder.setContentIntent(pendingIntent);
Notification notification = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
notification = builder.build();
} else {
notification = builder.getNotification();
}
//把该service创建为前台service
startForeground(startId, notification);
thread.start();
return START_STICKY;
}
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
boolean b = ServiceUtils.isServiceWork(Service1.this, "com.service.Service2");
if(!b) {
Intent service = new Intent(Service1.this, Service2.class);
startService(service);
}
}
};
timer.schedule(task, 0, 1000);
}
});
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
public class Service2 extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
thread.start();
return START_REDELIVER_INTENT;
}
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
boolean b = ServiceUtils.isServiceWork(MyJobService.this, "com.service.Service1");
if(!b) {
Intent service = new Intent(Service2.this, Service1.class);
startService(service);
}
}
};
timer.schedule(task, 0, 1000);
}
});
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
package com.zigsun.util;
import android.app.ActivityManager;
import android.content.Context;
import java.util.List;
/**
* Created by Seal on 2017/8/14.
*/
public class ServiceUtils {
/**
* 判断某个服务是否正在运行的方法
*
* @param mContext
* @param serviceName
* 是包名+服务的类名
* @return
*/
public static boolean isServiceWork(Context mContext, String serviceName) {
boolean isWork = false;
ActivityManager myAM = (ActivityManager) mContext
.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> myList = myAM.getRunningServices(100);
if (myList.size() <= 0) {
return false;
}
for (int i = 0; i < myList.size(); i++) {
String mName = myList.get(i).service.getClassName().toString();
if (mName.equals(serviceName)) {
isWork = true;
break;
}
}
return isWork;
}
/**
* 判断进程是否运行
*
* @return
*/
public static boolean isProessRunning(Context context, String proessName) {
boolean isRunning = false;
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> lists = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo info : lists) {
if (info.processName.equals(proessName)) {
isRunning = true;
}
}
return isRunning;
}
}
在Activity中启用Service:
private void initService() {
boolean isRun2 = ServiceUtils.isServiceWork(SplashActivity.this,
"com.service.Service1");
if (isRun2 == false) {
startService(new Intent(SplashActivity.this, Service1.class));
try {
} catch (Exception e) {
e.printStackTrace();
}
}
boolean isRun1 = ServiceUtils.isServiceWork(SplashActivity.this,
"com.service.Service2");
if (isRun1 == false) {
startService(new Intent(SplashActivity.this, Service2.class));
try {
} catch (Exception e) {
e.printStackTrace();
}
}
}