http://blog.csdn.net/huxueyan521/article/details/8921976

做一个应用,需要强制关闭进程。

可以使用ActivityManager的killBackgroundProcesses方法,需要权限android.permission.KILL_BACKGROUND_PROCESSES。但使用此方法杀死进程后,进程会重启。源码中解释如下:

Have the system immediately kill all background processes associated with the given package.  This is the same as the kernel killing those processes to reclaim memory; the system will take care of restarting these processes in the future as needed.


为了强制关闭进程,希望使用ActivityManager的另外一个方法,forceStopPackage。源码中解释如下:

Have the system perform a force stop of everything associated with the given application package.  All processes that share its uid will be killed, all services it has running stopped, all activities removed, etc.  In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED} broadcast will be sent, so that any of its registered alarms can be stopped, notifications removed, etc.

使用这个方法有两点需要注意:

- 此方法是@hide的方法:

解决方案是使用java的反射机制完成调用,代码如下:

ActivityManager mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
Method method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class);
method.invoke(mActivityManager, packageName);  //packageName是需要强制停止的应用程序包名

- 此方法需要权限:android.permission.FORCE_STOP_PACKAGES

下面着手分析这个权限。

这个权限在frameworks/base/core/res/AndroidManifest.xml文件中声明,如下:


 android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
 android:protectionLevel="signature"
 android:label="@string/permlab_forceStopPackages"
 android:description="@string/permdesc_forceStopPackages"/>

注意protectionLevel属性值未signature。看sdk文档 http://developer.android.com/guide/topics/manifest/permission- element.html#plevel中对这一属性的解释如下:

A permission that the system grants only if the requesting application is signed with the same certificate as the application that declared the permission. If the certificates match, the system automatically grants the permission without notifying the user or asking for the user's explicit approval.

意思是:app使用FORCE_STOP_PACKAGES权限,app必须和这个权限的声明者的签名保持一致!

FORCE_STOP_PACKAGES的声明者是frameworks/base/core/res/,可以在frameworks/base/core/res/Android.mk中看到它的签名信息:

LOCAL_NO_STANDARD_LIBRARIES := true
LOCAL_PACKAGE_NAME := framework-res
LOCAL_CERTIFICATE := platform

即,签名为platform.

最终得到结论,app需要是platform签名,才可以使用forceStopPackage方法!

网上有很多文章提及,需要在app的AndroidManifest.xml中添加 android:sharedUserId="android.uid.system"一句话。看 sdk(http://developer.android.com/guide/topics/manifest/manifest- element.html)对此的解释:

android:sharedUserId The name of a Linux user ID that will be shared with other applications. By default, Android assigns each application its own unique user ID. However, if this attribute is set to the same value for two or more applications, they will all share the same ID — provided that they are also signed by the same certificate. Application with the same user ID can access each other's data and, if desired, run in the same process.

意思是,两个app使用了相同的user id,就可以互相访问对方的数据。因此,app使用android.uid.system的user id,就可以访问系统数据。注意背景为黄色的一句,这里依然需要两个app有相同的签名才行。

posted @ 2016-06-13 09:44 精灵化石 阅读(2630) 评论(0) 推荐(0) 编辑
摘要: lucene是什么?lucene是一个开源的,广泛应用的,对数据进行索引、查询的一个框架,更详细的介绍请查看www.lucene.com.下面简单的描述一下索引和查询过程。1. 做索引简单过程://获取索引存储路径String strindexDir =“”;File indexDir = new ... 阅读全文
posted @ 2015-04-04 10:38 精灵化石 阅读(566) 评论(0) 推荐(0) 编辑
摘要: 本文记录Lucene+Paoding的使用方法图解:一、下载Lucene(官网:http://archive.apache.org/dist/lucene/java/)本文中使用的是:2.9.4,下载后解压,Lucene所需要的基本jar文件如下列表: lucene-core-2.9.4.jar L... 阅读全文
posted @ 2015-04-04 10:36 精灵化石 阅读(293) 评论(0) 推荐(0) 编辑
摘要: Lucene 简介Lucene 是一个基于 Java 的全文信息检索工具包,它不是一个完整的搜索应用程序,而是为你的应用程序提供索引和搜索功能。Lucene 目前是 Apache Jakarta 家族中的一个开源项目。也是目前最为流行的基于 Java 开源全文检索工具包。目 前已经有很多应用程序的搜... 阅读全文
posted @ 2015-04-04 10:31 精灵化石 阅读(202) 评论(0) 推荐(0) 编辑
摘要: 1.将字符串的时间转换为时间戳方法:a = "2013-10-10 23:40:00"将其转换为时间数组importtimetimeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")转换为时间戳:timeStamp = int(time.mktime(time... 阅读全文
posted @ 2015-03-05 08:59 精灵化石 阅读(2981) 评论(0) 推荐(1) 编辑
摘要: 通过对前面的一篇博文的学习,我们掌握了Activity组件布局文件地创建过程以及其顶层控件DecorView,今天我们继续庖丁解牛---深入到其中的generateLayout()方法,步步为营掌握一下内容: 1、Activity中Theme(主题)的系统定义以及使用之处; 2、如... 阅读全文
posted @ 2015-02-03 14:58 精灵化石 阅读(9113) 评论(0) 推荐(0) 编辑
摘要: 修改Gallery2记录1. 修改ActionBar的背景色,本例为修改为红色#ffff0000,也可以指定到/res/drawable/xx.png1). 在Gallery2/res/values/ 增加自己的资源文件theme.xml[html]view plaincopy2). 在Androi... 阅读全文
posted @ 2015-02-02 15:05 精灵化石 阅读(1218) 评论(0) 推荐(0) 编辑
摘要: 学习了半个月的Android,一直都是在虚拟平台上调试,今天想在真实的机子里面跑跑自己写的程序与在虚拟平台上有什么不同,为了学习Android,自己可是特别的买了个Android的手机,华为的C8650,这个手机什么都好,就是只能用电信的东西不爽,而且也是由于这个问题,害我在Ubuntu里面配置真机开发环境的时候吃尽了苦头。 按照Android developer网站上说的一样,在Linux环境下Android的真机调试不能想Windows 一样装个驱动什么的就得了,在Linux里需要建立一个.rules文件,告诉Linux在Android手机的USB插入电脑的时候执行的规则。具体步骤如... 阅读全文
posted @ 2012-02-09 15:14 精灵化石 阅读(1896) 评论(0) 推荐(0) 编辑
摘要: Android 手机开发软件,因为不同区域人的使用需要一个国际化的过程。在Android手机开发中,国际话的非常的简单,主要还是在res的文件中下功夫。 在android的项目文件中的res文件夹下,默认生成了软件在手机环境为任何情况下显示的文字鱼图片,而如果我们需要当用户修改了手机的语言环境下自动加载对应的语言的资源文件,只需要创建该语言对应的文件夹和文件即可,比如我们使用的文字,Android默认文字的资源存放在values文件的string.xml文件中,如果我们需要一个中文与英文的文字的资源文件,只需要在res文件夹中创建一个名为values-zh和values-en的文件夹,然后在文 阅读全文
posted @ 2012-02-09 15:00 精灵化石 阅读(1177) 评论(1) 推荐(0) 编辑
摘要: Android 手机开发过程中,理解好Activity的生命周期以及某种状态执行那一个方法在开发中有着事半功倍的作用。Activity 的生命周期可以主要是从创建,暂停,挂起,销毁的过程,如下图:下面通过一个音乐播放器的例子加深自己的记忆。音乐播放器界面:布局的代码如下:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout 阅读全文
posted @ 2012-02-09 14:59 精灵化石 阅读(1734) 评论(0) 推荐(1) 编辑
点击右上角即可分享
微信分享提示