android中使用adb shell命令kill掉应用程序

Android测试中,经常需要kill掉应用程序。

  网上有用方法:adb shell中的kill命令后面跟的参数是pid,因此先ps所有的应用程序。缺点是,PS显示很长,找到你的应用比较麻烦。

  另一种方法是使用包名,比较简单:

  C:\Android\adt-bundle-windows-x86-20130729\sdk\platform-tools>adb shell  am force-stop 包名 

   比如 com.amap。


附录参考:

am命令,am全称activity manager,你能使用am去模拟各种系统的行为,例如去启动一个activity,强制停止进程,发送广播进程,修改设备屏幕属性等等

命令窗口通过adb shell 进入android 的Linux命令界面,输入am -help看到如下信息:


force-stop <PACKAGE> : 强制停止指定的package包应用。

kill [options] <PACKAGE> :杀死指定package包应用进程,该命令在安全模式下杀死进程,不影响用户体验。参数选项:--user <USER_ID> | all | current: 指定user进程杀死,如果不指定默认为所有users。(关于USER_ID下面会介绍到)

kill-all :杀死所有的后台进程。

broadcast [options] <INTENT> :发送一个intent。具体intent参数参照start命令参数。参数选项:--user <USER_ID> | all | current: 指定user进程杀死,如果不指定默认为所有users。

instrument [options] <COMPONENT> :测试命令,不多作介绍。

profile start <PROCESS> <FILE> :在<PROCESS>进程中运行profile,分析结果写到<FILE>里。

profile stop <PROCESS> :停止profile。

set-debug-app [options] <PACKAGE> :设置package包应用为debug模式。参数选项:-w|--persistent:等待进入调试模式,保留值。

clear-debug-app :清空之前用set-debug-app命令设置的package包应用。

以下命令查看官网:
monitor [options]
screen-compat [on|off] <PACKAGE>
display-size [reset|<WxH>]
display-density <dpi>
to-uri <INTENT>
to-intent-uri <INTENT>





我们可以通过命令启动android中的Activity,Service,BroadcastReceiver 等组件

 

1. 拨打一个电话:

 

    am start -a android.intent.action.CALL -d tel:10086

 

    这里-a表示动作,-d表述传入的数据,还有-t表示传入的类型。

 

2. 打开一个网页:

 

    am start -a android.intent.action.VIEW -d  http://www.baidu.com (这里-d表示传入的data)

 

3. 打开音乐播放器:

 

    am start -a android.intent.action.MUSIC_PLAYER 或者

    am start -n com.android.music/om.android.music.MusicBrowserActivity

 

4. 启动一个服务:

 

    am startservice <服务名称>

 

    例如:am startservice -n com.android.music/com.android.music.MediaPlaybackService (这里-n表示组件)

    或者   am startservice -a com.smz.myservice (这里-a表示动作,就是你在Androidmanifest里定义的) 

 

5. 发送一个广播:

 

    am broadcast -a <广播动作>

 

    例如: am broadcast -a com.smz.mybroadcast

 



6 启动一个Activity 

# am start -n 包(package)名/包名.活动(activity)名称

启动的方法可以从每个应用的AndroidManifest.xml的文件中得到

 

Music 和 Video(音乐和视频)的启动方法为:

# am start -n com.android.music/com.android.music.MusicBrowserActivity

# am start -n com.android.music/com.android.music.VideoBrowserActivity

# am start -n com.android.music/com.android.music.MediaPlaybackActivity

 

Camera(照相机)的启动方法为:

# am start -n com.android.camera/com.android.camera.Camera

 

Browser(浏览器)的启动方法为:

# am start -n com.android.browser/com.android.browser.BrowserActivity

 

启动 google map 直接定位到北京 :

am start -a android.intent.action.VIEW geo:0,0?q=beijing


am还有很多的用法,有待研究。

接下来介绍pm命令,pm全称package manager,你能使用pm命令去模拟android行为或者查询设备上的应用等,当你在adb shell命令下执行pm命令:

pm <command>
你也可以在adb shell前执行pm命令:
adb shell pm uninstall com.example.MyApp

关于一些pm命令的介绍:
list packages [options] <FILTER> :打印所有包,选择性的查询包列表。参数选项:-f:查看关联文件,即应用apk的位置跟对应的包名(如:package:/system/app/MusicPlayer.apk=com.sec.android.app.music);-d:查看disabled packages;-e:查看enable package;-s:查看系统package;-3:查看第三方package;-i:查看package的对应安装者(如:1、package:com.tencent.qqmusic installer=null 2、package:com.tencent.qqpim installer=com.android.vending);-u:查看曾被卸载过的package。(卸载后又重新安装依然会被列入);--user<USER_ID>:The user space to query。

list permission-groups :打印所有已知的权限群组。

list permissions [options] <GROUP> :选择性的打印权限。参数选项:

list features :设备特性。硬件之类的性能。

list libraries :当前设备支持的libs。

list users :系统上所有的users。(上面提到的USER_ID查询方式,如:UserInfo{0:Primary:3}那么USER_ID为0)

path <PACKAGE> :查询package的安装位置。

install [options] <PATH> :安装命令。

uninstall [options] <PACKAGE> :卸载命令。

clear <PACKAGE> :对指定的package删除所有数据。

enable <PACKAGE_OR_COMPONENT> :使package或component可用。(如:pm enable \" package/class\" )

disable <PACKAGE_OR_COMPONENT> :使package或component不可用。(如:pm disable \" package/class\" )

disable-user [options] <PACKAGE_OR_COMPONENT> :参数选项:--user <USER_ID>: The user to disable.
grant <PACKAGE_PERMISSION> :授权给应用。

revoke <PACKAGE_PERMISSION> :撤销权限。

set-install-location <LOCATION> :设置默认的安装位置。其中0:让系统自动选择最佳的安装位置。1:安装到内部的设备存储空间。2:安装到外部的设备存储空间。(这只用于调试应用程序,使用该命令可能导致应用程序退出或者其他不适的后果)。

get-install-location :返回当前的安装位置。返回结果同上参数选项。

set-permission-enforced <PERMISSION> [true|false] :使指定权限生效或者失效。

create-user <USER_NAME> :增加一个新的USER。

remove-user <USER_ID> :删除一个USER。

get-max-users :该设备所支持的最大USER数。(某些设备不支持该命令)



http://www.07net01.com/program/577315.html


android中使用adb shell命令kill掉应用程序

http://www.cppblog.com/AutomateProgram/archive/2010/06/10/117595.aspx

由于adb shell中的kill命令后面跟的参数是pid,因此先ps所有的应用程序:

adb在shell下:

ps

USER     PID   PPID  VSIZE RSS   WCHAN    PC         NAME

app_9    720   553   94476 14524 ffffffff afe0d3e4 S com.android.music

例如要关掉music

# kill 720
kill 720



[Android] Android杀死进程方法


 1. android.os.Process.killProcess(pid)

  只能终止本程序的进程,无法终止其它的

  具体代码如下:

  Process.killProcess(Process.myPid());
Process.killProcess(Process.myPid());

  2. System.exit(int code)

  例如: System.exit(0);

  该方法只能用于结束当前进程自身,在程序遇到异常,无法正常执行时,可以 通过这个方法强制退出。 

  需要注意的是: android.os.Process.killProcess(pid) 和 System.exit(int code)会导致进程非正常退出,进程退出时不会去执行Activity的onPause、onStop和onDestroy方法,那么进程很有可能错过了保存数据的机会。因此,这两个方法最好使用在出现异常的时候!大家需要注意其使用方法。

     3. Android 结束进程,关闭程序的方法 即采用下面这个类

  使用这个类的具体源代码

final ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);   
am.restartPackage(getPackageName());

   不要忘记了在配置文件中设置权限:

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

  4. 在android2.2版本之后则不能再使用restartPackage()方法,而应该使用killBackgroundProcesses()方法

  manager.killBackgroundProcesses(getPackageName());

  使用示例代码如下:

ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);   
manager.killBackgroundProcesses(getPackageName());

//需要在xml中加入权限声明    

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


注意:第一种和第一种只能结束自身进程,无法结束其它进程;第三种和第四种只能结束其它进程,不能结束自身进程;


使用ActivityManager的forceStopPackage方法结束进程

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

可以使用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的反射机制完成调用,代码如下:

[java]  view plain  copy
 print ?
  1. ActivityManager mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);  
  2. Method method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class);  
  3. method.invoke(mActivityManager, packageName);  //packageName是需要强制停止的应用程序包名  

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

下面着手分析这个权限。

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

[java]  view plain  copy
 print ?
  1.   
[html]  view plain  copy
 print ?
  1. <permission android:name="android.permission.FORCE_STOP_PACKAGES"  
  2.  android:permissionGroup="android.permission-group.SYSTEM_TOOLS"  
  3.  android:protectionLevel="signature"  
  4.  android:label="@string/permlab_forceStopPackages"  
  5.  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中看到它的签名信息:

[html]  view plain  copy
 print ?
  1. LOCAL_NO_STANDARD_LIBRARIES :true  
  2. LOCAL_PACKAGE_NAME :framework-res  
  3. 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有相同的签名才行。

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值