zoukankan      html  css  js  c++  java
  • 取得正在运行的服务



    在main.xml中:

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout

      xmlns:android="http://schemas.android.com/apk/res/android"

      android:orientation="vertical"

      android:layout_width="fill_parent"

      android:layout_height="fill_parent"

      android:background="#3399ff">

      <ListView

         android:id="@+id/tasklist"

         android:layout_gravity="center_horizontal"

         android:layout_width="fill_parent"

         android:layout_height="wrap_content" />

    </LinearLayout>

    在MyActivityRun.java中:

    package com.li.activityrun;

    import java.util.ArrayList;

    import java.util.Iterator;

    import java.util.List;

    import android.app.Activity;

    import android.app.ActivityManager;

    import android.content.Context;

    import android.os.Bundle;

    import android.widget.ArrayAdapter;

    import android.widget.ListAdapter;

    import android.widget.ListView;

    public class MyActivityRun extends Activity {

      private ListView tasklist = null ;

      private ListAdapter adapter = null ;

      private List<String> all = new ArrayList<String>() ;

      private ActivityManager activityManager = null ;

      private List<ActivityManager.RunningServiceInfo> allTaskInfo ;

      @Override

      public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         super.setContentView(R.layout.main);

         this.tasklist = (ListView) super.findViewById(R.id.tasklist) ;

         this.activityManager = (ActivityManager) super

             .getSystemService(Context.ACTIVITY_SERVICE);

         this.listActivity() ;

      }

      private void listActivity() {

        this.allTaskInfo = this.activityManager.getRunningServices(30) ;

         Iterator<ActivityManager.RunningServiceInfo> iterInfo = this.allTaskInfo.iterator() ;

         while(iterInfo.hasNext()) {

           ActivityManager.RunningServiceInfo service = iterInfo.next() ;

           this.all.add("【ID = " + service.pid + " 】 "

                + service.process);

         }

         this.adapter = new ArrayAdapter<String>(this,

             android.R.layout.simple_list_item_1, this.all);

         this.tasklist.setAdapter(this.adapter) ;

      }

    }

    在AndroidManifest.xml中修改权限:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"

        package="com.li.activityrun"

        android:versionCode="1"

        android:versionName="1.0" >

        <uses-sdk

            android:minSdkVersion="8"

            android:targetSdkVersion="15" />

        <uses-permission android:name="android.permission.GET_TASKS"/>

        <application

            android:icon="@drawable/ic_launcher"

            android:label="@string/app_name"

            android:theme="@style/AppTheme" >

            <activity

                android:name=".MyActivityRun"

                android:label="@string/title_activity_my_activity_run" >

                <intent-filter>

                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />

                </intent-filter>

            </activity>

        </application>

    </manifest>

  • 相关阅读:
    对用户控件(ascx)属性(property)赋值
    The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
    图片淡入淡出切换效果
    在用户控件(ASCX)创建用户控件(ASCX)
    Login failed for user 'xxx'
    一些较好的书
    儿子购买的书
    怀念以前做网管的日子
    Linux下selinux简单梳理
    Rsync同步时删除多余文件 [附:删除大量文件方法的效率对比]
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3313124.html
Copyright © 2011-2022 走看看