EX05_21.java
上传用户:vip_99
上传日期:2021-03-27
资源大小:61159k
文件大小:4k
源码类别:

android开发

开发平台:

Java

  1. package irdc.ex05_21;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.app.ActivityManager;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.AdapterView;
  9. import android.widget.ArrayAdapter;
  10. import android.widget.Button;
  11. import android.widget.ListView;
  12. import android.widget.Toast;
  13. public class EX05_21 extends Activity
  14. {
  15.   private Button mButton01;
  16.   private ListView mListView01;
  17.   private ArrayAdapter<String> aryAdapter1;
  18.   private ArrayList<String> arylistTask;
  19.   
  20.   /* 类成员设置取回最多几笔的Task数量 */
  21.   private int intGetTastCounter=30;
  22.   
  23.   /* 类成员ActivityManager对象 */
  24.   private ActivityManager mActivityManager;
  25.   
  26.   /** Called when the activity is first created. */
  27.   @Override
  28.   public void onCreate(Bundle savedInstanceState)
  29.   {
  30.     super.onCreate(savedInstanceState);
  31.     setContentView(R.layout.main);
  32.     
  33.     mButton01 = (Button)findViewById(R.id.myButton1);
  34.     mListView01 = (ListView)findViewById(R.id.myListView1);
  35.     
  36.     /* 点击按钮取得正在后台运行的工作程序 */
  37.     mButton01.setOnClickListener(new Button.OnClickListener()
  38.     {
  39.       @Override
  40.       public void onClick(View v)
  41.       {
  42.         // TODO Auto-generated method stub
  43.         try
  44.         {
  45.           /* ActivityManager对象向系统取得ACTIVITY_SERVICE */
  46.           mActivityManager = (ActivityManager)
  47.           EX05_21.this.getSystemService(ACTIVITY_SERVICE);
  48.           
  49.           arylistTask = new ArrayList<String>();
  50.           
  51.           /* 以getRunningTasks方法取回正在运行中的程序TaskInfo */
  52.           List<ActivityManager.RunningTaskInfo> mRunningTasks = 
  53.           mActivityManager.getRunningTasks(intGetTastCounter);
  54.           
  55.           int i = 1;
  56.           /* 以循环及baseActivity方式取得工作名称与ID */ 
  57.           for (ActivityManager.RunningTaskInfo amTask : mRunningTasks)
  58.           {
  59.             /* baseActivity.getClassName取出运行工作名称 */
  60.             arylistTask.add("" + (i++) + ": "+ 
  61.             amTask.baseActivity.getClassName()+
  62.             "(ID=" + amTask.id +")");
  63.           }
  64.           aryAdapter1 = new ArrayAdapter<String>
  65.           (EX05_21.this, R.layout.simple_list_item_1, arylistTask);
  66.           
  67.           if(aryAdapter1.getCount()==0)
  68.           {
  69.             /* 当没有任何运行的工作,则提示信息 */
  70.             mMakeTextToast
  71.             (
  72.               getResources().getText
  73.               (R.string.str_err_no_running_task).toString(),
  74.               true
  75.             );
  76.           }
  77.           else
  78.           {
  79.             /* 发现后台运行的工作程序,以ListView Widget条列呈现 */
  80.             mListView01.setAdapter(aryAdapter1);
  81.           }
  82.         }
  83.         catch(SecurityException e)
  84.         {
  85.           /* 当无GET_TASKS权限时(SecurityException异常)提示信息 */
  86.           mMakeTextToast
  87.           (
  88.             getResources().getText
  89.             (R.string.str_err_permission).toString(),
  90.             true
  91.           );
  92.         }
  93.       }
  94.     });
  95.     
  96.     /* 当User在运行工作选择时的事件处理 */
  97.     mListView01.setOnItemSelectedListener
  98.     (new ListView.OnItemSelectedListener()
  99.     {
  100.       @Override
  101.       public void onItemSelected
  102.       (AdapterView<?> parent, View v, int id, long arg3)
  103.       {
  104.         // TODO Auto-generated method stub
  105.         /* 由于将运行工作以数组存放,故以id取出数组元素名称 */
  106.         mMakeTextToast(arylistTask.get(id).toString(),false);
  107.       }
  108.       @Override
  109.       public void onNothingSelected(AdapterView<?> arg0)
  110.       {
  111.         // TODO Auto-generated method stub
  112.         
  113.       }
  114.     });
  115.     
  116.     /* 当User在运行工作上点击时的事件处理 */
  117.     mListView01.setOnItemClickListener
  118.     (new ListView.OnItemClickListener()
  119.     {
  120.       @Override
  121.       public void onItemClick
  122.       (AdapterView<?> parent, View v, int id,  long arg3)
  123.       {
  124.         // TODO Auto-generated method stub
  125.         /* 由于将运行工作以数组存放,故以id取出数组元素名称 */
  126.         mMakeTextToast(arylistTask.get(id).toString(), false);
  127.       }
  128.     });
  129.   }
  130.   
  131.   public void mMakeTextToast(String str, boolean isLong)
  132.   {
  133.     if(isLong==true)
  134.     {
  135.       Toast.makeText(EX05_21.this, str, Toast.LENGTH_LONG).show();
  136.     }
  137.     else
  138.     {
  139.       Toast.makeText(EX05_21.this, str, Toast.LENGTH_SHORT).show();
  140.     }
  141.   }
  142. }