Get Running Services Info : Service « Network « Android






Get Running Services Info

  
package app.test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;

class CMDExecute {

  public synchronized String run(String[] cmd, String workdirectory)
      throws IOException {
    String result = "";

    try {
      ProcessBuilder builder = new ProcessBuilder(cmd);
      // set working directory
      if (workdirectory != null)
        builder.directory(new File(workdirectory));
      builder.redirectErrorStream(true);
      Process process = builder.start();
      InputStream in = process.getInputStream();
      byte[] re = new byte[1024];
      while (in.read(re) != -1) {
        System.out.println(new String(re));
        result = result + new String(re);
      }
      in.close();

    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return result;
  }

}

public class Main {
  private static StringBuffer buffer;
  // formart data long to string
  public static String formatData(long data) {
    Date d = new Date(data);
    DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String str = format2.format(d);
    return str;
  }

  // RunningServicesInfo
  public static String getRunningServicesInfo(Context context) {
    StringBuffer serviceInfo = new StringBuffer();
    final ActivityManager activityManager = (ActivityManager) context
        .getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningServiceInfo> services = activityManager
        .getRunningServices(100);

    Iterator<RunningServiceInfo> l = services.iterator();
    while (l.hasNext()) {
      RunningServiceInfo si = (RunningServiceInfo) l.next();
      serviceInfo.append("pid: ").append(si.pid);
      serviceInfo.append("\nprocess: ").append(si.process);
      serviceInfo.append("\nservice: ").append(si.service);
      serviceInfo.append("\ncrashCount: ").append(si.crashCount);
      serviceInfo.append("\nclientCount: ").append(si.clientCount);
      serviceInfo.append("\nactiveSince: ").append(
          formatData(si.activeSince));
      serviceInfo.append("\nlastActivityTime: ").append(
          formatData(si.lastActivityTime));
      serviceInfo.append("\n\n");
    }
    return serviceInfo.toString();
  }
}

   
    
  








Related examples in the same category

1.Map Service
2.Widget Service
3.Restful web service task
4.Search with Restful service
5.Post Restful service
6.Tracker Service
7.Weather web service
8.Access a Web service using GET
9.Alarm service
10.Local service demo
11.extends Service
12.Service structure
13.Clock Back Service
14.Voice Recognition Service
15.is Connected by Context.CONNECTIVITY_SERVICE
16.BeatService extends Service and Thread
17.TimeoutService (beta). Here you can register a timeout.
18.Timeout Service
19.Screenshot Service