第150页
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
(5)启动应用程序,界面如图13.11所示。在应用程序启动1分钟后会显示提示信息,单击打开后如图13.12所示。
图13.11 应用程序主界面 图13.12 显示提示信息
13.5.2 查看当前运行服务信息
例13.6 在Eclipse中创建Android项目,名称为13.6,实现在Activity中显示当前运行服务的详细信息功能。(实例位置:光盘\TM\sl\13\13.6)
(1)在com.mingrisoft包中创建ServicesListActivity类,它继承了Activity类。在onStart()方法中,获得当前正在运行服务的列表。对于每个服务,获得其详细信息并在Activity中输出,其代码如下:
public class ServicesListActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onStart() {
super.onStart();
StringBuilder serviceInfo = new StringBuilder();
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List services = manager.getRunningServices(100);//获得正在运行的服务列表
for (Iterator it = services.iterator(); it.hasNext();) {
RunningServiceInfo info = it.next();
//获得一个服务的详细信息并保存到StringBuilder
serviceInfo.append("activeSince: " + formatData(info.activeSince) + "\n");
serviceInfo.append("clientCount: " + info.clientCount + "\n");
serviceInfo.append("clientLabel: " + info.clientLabel + "\n");
serviceInfo.append("clientPackage: " + info.clientPackage + "\n");
serviceInfo.append("crashCount: " + info.crashCount + "\n");
serviceInfo.append("flags: " + info.flags + "\n");
serviceInfo.append("foreground: " + info.foreground + "\n");
serviceInfo.append("lastActivityTime: " + formatData(info.lastActivityTime) + "\n");
serviceInfo.append("pid: " + info.pid + "\n");
serviceInfo.append("process: " + info.process + "\n");
serviceInfo.append("restarting: " + formatData(info.restarting) + "\n");
serviceInfo.append("service: " + info.service + "\n");
serviceInfo.append("started: " + info.started + "\n");
serviceInfo.append("uid: " + info.uid + "\n");
serviceInfo.append("\n");
}
ScrollView scrollView = new ScrollView(this); //创建滚动视图
TextView textView = new TextView(this); //创建文本视图
textView.setBackgroundColor(Color.BLACK); //设置文本颜色
textView.setTextSize(25); //设置字体大小
textView.setText(serviceInfo.toString()); //设置文本内容
scrollView.addView(textView); //将文本视图增加到滚动视图
setContentView(scrollView); //显示滚动视图
}
private static String formatData(long data) { //用于格式化时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(new Date(data));
}
}
(2)修改AndroidManifest.xml文件,增加Activity和Service配置,其代码如下:
package="com.mingrisoft"
android:versionCode="1"
android:versionName="1.0" >