Example usage for android.app ActivityManager getRunningServices

List of usage examples for android.app ActivityManager getRunningServices

Introduction

In this page you can find the example usage for android.app ActivityManager getRunningServices.

Prototype

@Deprecated
public List<RunningServiceInfo> getRunningServices(int maxNum) throws SecurityException 

Source Link

Document

Return a list of the services that are currently running.

Usage

From source file:anomalyDetector.activities.MainFeaturesActivity.java

@Override
protected void onStart() {
    super.onStart();

    selectedFeatures = new ArrayList<String>();
    /*//from  w  w w. j  a  va  2 s.c om
     * Check if the service is running
     */
    ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> rs = am.getRunningServices(100);
    for (ActivityManager.RunningServiceInfo rsi : rs) {
        String serviceClassName = rsi.service.getClassName();
        if (serviceClassName.equals("anomalyDetector.services.ColectFeaturesService")) {
            isServiceRunning = true;
            drawerItemClickListener.setServiceRunning(true);
            break;
        }
    }

    // Enable or disable the buttons based on service status(running or not running)
    if (isServiceRunning) {
        startServiceButton.setEnabled(false);
        stopServiceButton.setEnabled(true);
    } else {
        startServiceButton.setEnabled(true);
        stopServiceButton.setEnabled(false);
    }

    /*
     * List of features that can be collected
     */
    AssetManager assetManager = getAssets();
    BufferedReader reader;

    /*
     * Read the features from assets and add them to the arraylist
     */
    try {
        reader = new BufferedReader(new InputStreamReader(assetManager.open("features.txt")));
        String line;
        while ((line = reader.readLine()) != null) {
            // Skip comments
            if (line.startsWith("#")) {
                continue;
            }
            selectedFeatures.add(line);
        }

        reader.close();

    } catch (IOException e) {
        Log.d("MainFeaturesActivity", "Error reading assets file");
    }

    // Create an adapter that holds the selected features
    // and pass it to the list view
    listViewAdapter = new ArrayAdapter<String>(this, R.layout.row, selectedFeatures);
    listView.setAdapter(listViewAdapter);
}

From source file:com.poloure.simplerss.FeedsActivity.java

private boolean isServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (ServiceUpdate.class.getName().equals(service.service.getClassName())) {
            return true;
        }//from   w  w  w . ja  v  a2  s  .  c  o m
    }
    return false;
}

From source file:com.a3did.partner.recosample.MainActivity.java

private boolean isBackgroundMonitoringServiceRunning(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo runningService : am.getRunningServices(Integer.MAX_VALUE)) {
        if (RecoBackgroundMonitoringService.class.getName().equals(runningService.service.getClassName())) {
            return true;
        }/*  w w  w  .j av  a  2s  .  c  om*/
    }
    return false;
}

From source file:com.a3did.partner.recosample.MainActivity.java

private boolean isBackgroundRangingServiceRunning(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo runningService : am.getRunningServices(Integer.MAX_VALUE)) {
        if (RecoBackgroundRangingService.class.getName().equals(runningService.service.getClassName())) {
            return true;
        }// w  ww.ja  va 2s  .c  om
    }
    return false;
}

From source file:org.peercast.core.PeerCastServiceController.java

/**
 * ??PeerCast??OS?????true/*from   w  w  w  .  ja  v a 2 s.  co  m*/
 * 
 * @return
 */
public boolean isServiceRunning() {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningServiceInfo> services = activityManager.getRunningServices(256);
    for (RunningServiceInfo info : services) {
        if (CLASS_NAME_PEERCAST_SERVICE.equals(info.service.getClassName())) {
            return true;
        }
    }
    return false;
}

From source file:com.gbaldera.tipaypal.TipaypalModule.java

private Boolean isPayPalServiceRunning() {
    ActivityManager manager = (ActivityManager) TiApplication.getAppCurrentActivity()
            .getSystemService(Context.ACTIVITY_SERVICE);

    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (PayPalService.class.getName().equals(service.service.getClassName())) {
            return true;
        }//from  www.j a  v  a  2s . com
    }

    return false;
}

From source file:com.google.transporttracker.TrackerActivity.java

private boolean isServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }//from w  w w  .j  a  v  a2  s.  c  om
    }
    return false;
}

From source file:com.google.android.gms.location.sample.locationupdatesforegroundservice.LocationUpdatesService.java

/**
 * Returns true if this is a foreground service.
 *
 * @param context The {@link Context}./* w  ww.j  a  va  2s  .c  o  m*/
 */
public boolean serviceIsRunningInForeground(Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (getClass().getName().equals(service.service.getClassName())) {
            if (service.foreground) {
                return true;
            }
        }
    }
    return false;
}

From source file:com.vonglasow.michael.satstat.GpsEventReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);

    // some logic to use the pre-1.7 setting KEY_PREF_UPDATE_WIFI as a
    // fallback if KEY_PREF_UPDATE_NETWORKS is not set
    Set<String> fallbackUpdateNetworks = new HashSet<String>();
    if (sharedPref.getBoolean(Const.KEY_PREF_UPDATE_WIFI, false)) {
        fallbackUpdateNetworks.add(Const.KEY_PREF_UPDATE_NETWORKS_WIFI);
    }/*from ww  w  .j  ava  2 s.c om*/
    Set<String> updateNetworks = sharedPref.getStringSet(Const.KEY_PREF_UPDATE_NETWORKS,
            fallbackUpdateNetworks);

    if (intent.getAction().equals(Const.GPS_ENABLED_CHANGE)
            || intent.getAction().equals(Const.GPS_ENABLED_CHANGE)) {
        //FIXME: why are we checking for the same intent twice? Should on of them be GPS_FIX_CHANGE?
        // an application has connected to GPS or disconnected from it, check if notification needs updating
        boolean notifyFix = sharedPref.getBoolean(Const.KEY_PREF_NOTIFY_FIX, false);
        boolean notifySearch = sharedPref.getBoolean(Const.KEY_PREF_NOTIFY_SEARCH, false);
        if (notifyFix || notifySearch) {
            boolean isRunning = false;
            ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
                if (PasvLocListenerService.class.getName().equals(service.service.getClassName())) {
                    isRunning = true;
                }
            }
            if (!isRunning) {
                Intent startServiceIntent = new Intent(context, PasvLocListenerService.class);
                startServiceIntent.setAction(intent.getAction());
                startServiceIntent.putExtras(intent.getExtras());
                context.startService(startServiceIntent);
            }
        }
    } else if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)
            && updateNetworks.contains(Const.KEY_PREF_UPDATE_NETWORKS_WIFI)) {
        // change in WiFi connectivity, check if we are connected and need to refresh AGPS
        //FIXME: KEY_PREF_UPDATE_WIFI as fallback only
        NetworkInfo netinfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (netinfo == null)
            return;
        if (!netinfo.isConnected())
            return;
        //Toast.makeText(context, "WiFi is connected", Toast.LENGTH_SHORT).show();
        Log.i(this.getClass().getSimpleName(), "WiFi is connected");
        refreshAgps(context, true, false);
    } else if ((intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION))
            || (intent.getAction().equals(Const.AGPS_DATA_EXPIRED))) {
        // change in network connectivity or AGPS expiration timer fired
        boolean isAgpsExpired = false;
        if (intent.getAction().equals(Const.AGPS_DATA_EXPIRED)) {
            Log.i(this.getClass().getSimpleName(), "AGPS data expired, checking available networks");
            isAgpsExpired = true;
        }
        NetworkInfo netinfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE))
                .getActiveNetworkInfo();
        if (netinfo == null)
            return;
        if (!netinfo.isConnected())
            return;
        String type;
        if ((netinfo.getType() < ConnectivityManager.TYPE_MOBILE_MMS)
                || (netinfo.getType() > ConnectivityManager.TYPE_MOBILE_HIPRI)) {
            type = Integer.toString(netinfo.getType());
        } else {
            // specific mobile data connections will be treated as TYPE_MOBILE
            type = Const.KEY_PREF_UPDATE_NETWORKS_MOBILE;
        }
        if (!updateNetworks.contains(type))
            return;
        if (!isAgpsExpired)
            Log.i(this.getClass().getSimpleName(),
                    "Network of type " + netinfo.getTypeName() + " is connected");
        // Enforce the update interval if we were called by a network event
        // but not if we were called by a timer, because in that case the
        // check has already been done. (I am somewhat paranoid and don't
        // count on alarms not going off a few milliseconds too early.)
        refreshAgps(context, !isAgpsExpired, false);
    }
}

From source file:org.appspot.apprtc.my.MainActivity.java

public boolean isServiceRunningCheck() {
    ActivityManager manager = (ActivityManager) this.getSystemService(Activity.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if ("org.appspot.apprtc.my.ClientSocketService".equals(service.service.getClassName())) {
            return true;
        }/*  ww  w .j av a2 s .  c  o  m*/
    }
    return false;
}