import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.List;
public class ServiceUtils {
/**
* 判断服务是否已经正在运行
* @param mContext 上下文对象
* @param className Service类的全路径类名 "包名+类名" 如com.demo.test.MyService
* @return
*/
public static boolean isServiceRunning(Context mContext, String className) {
ActivityManager myManager = (ActivityManager) mContext
.getApplicationContext().getSystemService(
Context.ACTIVITY_SERVICE);
ArrayList<ActivityManager.RunningServiceInfo> runningService = (ArrayList<ActivityManager.RunningServiceInfo>) myManager
.getRunningServices(30);
for (int i = 0; i < runningService.size(); i++) {
if (runningService.get(i).service.getClassName().toString()
.equals(className)) {
return true;
}
}
return false;
}
/**
* 判断某个界面是否在前台
*
* @param context
* @param className 某个界面名称
*
*/
public static boolean isActivityForeground(Context context, String className) {
if (context == null || TextUtils.isEmpty(className)) {
return false;
}
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
if (list != null && list.size() > 0) {
ComponentName cpn = list.get(0).topActivity;
if (className.equals(cpn.getClassName())) {
return true;
}
}
return false;
}
}