天天看点

判断Service是否在运行

private boolean isServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if ("com.baidu.location.service.MyLocationService".equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
 @Override      
protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        if(this.isServiceRunning()){
            startLocation.setText(getString(R.string.stoplocation));
        }else{
            startLocation.setText(getString(R.string.startlocation));
        }

      
}