Jan 10, 2015

List running service of Android device

The method getRunningServices(int maxNum) of ActivityManager return a list of the services that are currently running. (Note: this method is only intended for debugging or implementing service management type user interfaces)


package com.example.androidgetrunning;

import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.text.method.ScrollingMovementMethod;
import android.app.ActivityManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //setContentView(R.layout.activity_main);
  
  ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);

  /*
   * maxNum: the maximum number of entries to return in the list. 
   * The actual number returned may be smaller, 
   * depending on how many services are running.
   */
  int maxNum = 100;
  List<ActivityManager.RunningServiceInfo> list = activityManager.getRunningServices(maxNum);
  
  StringBuilder info = new StringBuilder();
  info.append("android-coding.blogspot.com" + "\n\n");
  info.append("no. of running service: " + list.size() + "\n\n");
  for(int i=0; i<list.size(); i++){
   info.append(list.get(i).service + "\n\n");
  }
  
  TextView texView = new TextView(this);
  texView.setMovementMethod(new ScrollingMovementMethod());
  texView.setText(info);
  setContentView(texView);
 }

}

No comments:

Post a Comment

Infolinks In Text Ads