MonoDroid学习笔记(十三)—— 自制任务管理器,获取正在运行的程序与服务

Android操作系统并没有提供任务管理程序,无从得知后台有哪些程序正在运行,但提供了正在运行的服务列表。这次我们来用MonoDroid来获取手机中正在运行的程序及服务,用ListView列出来,点击ListView的项可以实现关闭程序或服务。

这里我没有实现实时获取,而是通过点击两个按钮时才进行获取,布局文件如下:

[xhtml]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.                 android:layout_width="fill_parent"  
  4.                 android:layout_height="fill_parent">  
  5.   <Button android:id="@+id/btnGetTask"  
  6.           android:text="正在运行的程序"  
  7.           android:layout_width="wrap_content"  
  8.           android:layout_height="wrap_content"/>  
  9.   <Button android:id="@+id/btnGetService"  
  10.           android:text="正在运行的服务"  
  11.           android:layout_width="wrap_content"  
  12.           android:layout_height="wrap_content"  
  13.           android:layout_toRightOf="@id/btnGetTask"/>  
  14.   <ListView android:id="@+id/lvTasks"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="fill_parent"  
  17.             android:layout_below="@id/btnGetTask"></ListView>  
  18. </RelativeLayout>  
 

要获取正在运行的程序,需使用ActivityManager类的GetRunningTasks方法,它的参数指定需要取出的任务数,由于资源有限,所以我们这里设置了最多取出30条任务:

[c-sharp]  view plain copy print ?
  1. public class Activity1 : Activity  
  2. {  
  3.     List<string> list = new List<string>();  
  4.     IList<ActivityManager.RunningTaskInfo> listRunningTasks;  
  5.     ActivityManager am;  
  6.     protected override void OnCreate(Bundle bundle)  
  7.     {  
  8.         base.OnCreate(bundle);  
  9.         SetContentView(Resource.Layout.Main);  
  10.         Button btnGetTask = FindViewById<Button>(Resource.Id.btnGetTask);  
  11.         Button btnGetService = FindViewById<Button>(Resource.Id.btnGetService);  
  12.         ListView lvTasks = FindViewById<ListView>(Resource.Id.lvTasks);  
  13.         am = this.GetSystemService(ActivityService) as ActivityManager;  
  14.         btnGetTask.Click += (sender, e) =>   
  15.         {  
  16.             contentShown = 1;  
  17.             try  
  18.             {  
  19.                 list.Clear();  
  20.                 lvTasks.Adapter = null;  
  21.                 listRunningTasks = am.GetRunningTasks(30);  
  22.                 int index = 1;  
  23.                 foreach (ActivityManager.RunningTaskInfo task in listRunningTasks)  
  24.                 {  
  25.                     list.Add(string.Format("{0}:{1}(ID={2})", index++, task.BaseActivity.ClassName, task.Id));  
  26.                 }  
  27.                 ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, list);  
  28.                 if (adapter.Count == 0)  
  29.                 {  
  30.                     Toast.MakeText(this"当前没有任何程序运行", ToastLength.Long).Show();  
  31.                 }  
  32.                 else  
  33.                 {  
  34.                     lvTasks.Adapter = adapter;  
  35.                 }  
  36.             }  
  37.             catch (SecurityException)  
  38.             {  
  39.                 Toast.MakeText(this"没有获取运行程序的权限", ToastLength.Long).Show();  
  40.             }  
  41.             catch (System.Exception ex)  
  42.             {  
  43.                 MessageBox.ShowErrorMessage(this, ex);  
  44.             }  
  45.         };  
  46.         lvTasks.ItemClick += (sender, e) =>   
  47.         {  
  48.                 MessageBox.Confirm(this"提示""确定关闭此程序吗?", (s2, e2) =>  
  49.                 {  
  50.                     am.RestartPackage(listRunningTasks[e.Position].BaseActivity.PackageName);  
  51.                     btnGetTask.PerformClick();  
  52.                 }, (s3, e3) => { });  
  53.         };  
  54.     }  
  55. }  
 

注意要获取任务,必需要在AndroidManifest.xml中添加GET_Tasks权限,而要关闭程序,我们这里使用的是ActivityManager类的RestartPackage方法,根据程序的包名称来关闭,所以要添加RESTART_PACKAGES权限:

[xhtml]  view plain copy print ?
  1. <uses-permission android:name="android.permission.GET_TASKS" />  
  2. <uses-permission android:name="android.permission.RESTART_PACKAGES" />  
 

效果图:

 

下面我们来实现获取运行中服务的效果。获取服务与获取程序差不多,只要把GetRunningTasks改为GetRunningServices即可,同样我们还是取出30条。由于是在同一个ListView中显示,在单击列表项时需要区分单击的是程序还是服务,所以我们增加一个字段contentShown加以区分,contentShown=1时表示程序,contentShown=2时表示服务,完整的代码如下:

[c-sharp]  view plain copy print ?
  1. public class Activity1 : Activity  
  2. {  
  3.     List<string> list = new List<string>();  
  4.     IList<ActivityManager.RunningTaskInfo> listRunningTasks;  
  5.     IList<ActivityManager.RunningServiceInfo> listRunningServices;  
  6.     ActivityManager am;  
  7.     int contentShown = 0;  
  8.     protected override void OnCreate(Bundle bundle)  
  9.     {  
  10.         base.OnCreate(bundle);  
  11.         SetContentView(Resource.Layout.Main);  
  12.         Button btnGetTask = FindViewById<Button>(Resource.Id.btnGetTask);  
  13.         Button btnGetService = FindViewById<Button>(Resource.Id.btnGetService);  
  14.         ListView lvTasks = FindViewById<ListView>(Resource.Id.lvTasks);  
  15.         am = this.GetSystemService(ActivityService) as ActivityManager;  
  16.         btnGetTask.Click += (sender, e) =>   
  17.         {  
  18.             contentShown = 1;  
  19.             try  
  20.             {  
  21.                 list.Clear();  
  22.                 lvTasks.Adapter = null;  
  23.                 listRunningTasks = am.GetRunningTasks(30);  
  24.                 int index = 1;  
  25.                 foreach (ActivityManager.RunningTaskInfo task in listRunningTasks)  
  26.                 {  
  27.                     list.Add(string.Format("{0}:{1}(ID={2})", index++, task.BaseActivity.ClassName, task.Id));  
  28.                 }  
  29.                 ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, list);  
  30.                 if (adapter.Count == 0)  
  31.                 {  
  32.                     Toast.MakeText(this"当前没有任何程序运行", ToastLength.Long).Show();  
  33.                 }  
  34.                 else  
  35.                 {  
  36.                     lvTasks.Adapter = adapter;  
  37.                 }  
  38.             }  
  39.             catch (SecurityException)  
  40.             {  
  41.                 Toast.MakeText(this"没有获取运行程序的权限", ToastLength.Long).Show();  
  42.             }  
  43.             catch (System.Exception ex)  
  44.             {  
  45.                 MessageBox.ShowErrorMessage(this, ex);  
  46.             }  
  47.         };  
  48.         btnGetService.Click += (sender, e) =>   
  49.         {  
  50.             contentShown = 2;  
  51.             try  
  52.             {  
  53.                 lvTasks.Adapter = null;  
  54.                 listRunningServices = am.GetRunningServices(30);  
  55.                 int index = 1;  
  56.                 list.Clear();  
  57.                 foreach (ActivityManager.RunningServiceInfo service in listRunningServices)  
  58.                 {  
  59.                     list.Add(string.Format("{0}:{1}(ID={2})", index++, service.Process, service.Pid));  
  60.                 }  
  61.                 ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, list);  
  62.                 if (adapter.Count == 0)  
  63.                 {  
  64.                     Toast.MakeText(this"当前没有任何服务运行", ToastLength.Long).Show();  
  65.                 }  
  66.                 else  
  67.                 {  
  68.                     lvTasks.Adapter = adapter;  
  69.                 }  
  70.             }  
  71.             catch (SecurityException)  
  72.             {  
  73.                 Toast.MakeText(this"没有获取运行服务的权限", ToastLength.Long).Show();  
  74.             }  
  75.             catch (System.Exception ex)  
  76.             {  
  77.                 MessageBox.ShowErrorMessage(this, ex);  
  78.             }  
  79.         };  
  80.         lvTasks.ItemClick += (sender, e) =>   
  81.         {  
  82.             if (contentShown == 1)  
  83.             {  
  84.                 MessageBox.Confirm(this"提示""确定关闭此程序吗?", (s2, e2) =>  
  85.                 {  
  86.                     am.RestartPackage(listRunningTasks[e.Position].BaseActivity.PackageName);  
  87.                     btnGetTask.PerformClick();  
  88.                 }, (s3, e3) => { });  
  89.             }  
  90.             else if (contentShown == 2)  
  91.             {  
  92.                 MessageBox.Confirm(this"提示""确定关闭此服务吗?", (s2, e2) =>  
  93.                 {  
  94.                     am.RestartPackage(listRunningServices[e.Position].Service.PackageName);  
  95.                     btnGetService.PerformClick();  
  96.                 }, (s3, e3) => { });  
  97.             }  
  98.         };  
  99.     }  
  100. }  
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值