ServiceManager

public static boolean isActivityRunning(Context mContext) {
        ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> info = activityManager.getRunningTasks(1);
        if (info != null && info.size() > 0) {
            ComponentName component = info.get(0).topActivity;
            if ("com.example.similarforward.MainActivity".endsWith(component.getClassName())) {
                return true;
            }
        }
        return false;
    }
public static boolean isServiceRunning(Context mContext) {
        boolean isRunning = false;
        ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(30);

        if (!(serviceList.size() > 0)) {
            return false;
        }

        for (int i = 0; i < serviceList.size(); i++) {
            if (serviceList.get(i).service.getClassName().equals("com.example.similarforward.SocketService") == true) {
                isRunning = true;

                break;
            }
        }
        return isRunning;
    }
//判断是否有网
        public static boolean isNetWork(Context context) {    
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            // 检查网络连接,如果无网络可用,就不需要进行连网操作等
            NetworkInfo info = manager.getActiveNetworkInfo();
            if (info == null || !manager.getBackgroundDataSetting()) {

                return false;
            } else {

                return true;
            }

        }

 

posted @ 2016-04-20 13:17  千古丶风流人物  阅读(198)  评论(0编辑  收藏  举报