Android:adb shell am命令行发送Activity/Service/Broadcast

原文地址:

http://blog.csdn.net/annkie/article/details/7896559

0.adb shell am命名行参数

 

[java] view plaincopy
 
  1. F:\>adb shell  
  2. # am  
  3. am  
  4. usage: am [subcommand] [options]  
  5.   
  6.     start an Activity: am start [-D] [-W] <INTENT>  
  7.         -D: enable debugging  
  8.         -W: wait for launch to complete  
  9.   
  10.     start a Service: am startservice <INTENT>  
  11.   
  12.     send a broadcast Intent: am broadcast <INTENT>  
  13.   
  14.     start an Instrumentation: am instrument [flags] <COMPONENT>  
  15.         -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)  
  16.         -e <NAME> <VALUE>: set argument <NAME> to <VALUE>  
  17.         -p <FILE>: write profiling data to <FILE>  
  18.         -w: wait for instrumentation to finish before returning  
  19.   
  20.     start profiling: am profile <PROCESS> start <FILE>  
  21.     stop profiling: am profile <PROCESS> stop  
  22.   
  23.     <INTENT> specifications include these flags:  
  24.         [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]  
  25.         [-c <CATEGORY> [-c <CATEGORY>] ...]  
  26.         [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]  
  27.         [--esn <EXTRA_KEY> ...]  
  28.         [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]  
  29.         [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]  
  30.         [-n <COMPONENT>] [-f <FLAGS>]  
  31.         [--grant-read-uri-permission] [--grant-write-uri-permission]  
  32.         [--debug-log-resolution]  
  33.         [--activity-brought-to-front] [--activity-clear-top]  
  34.         [--activity-clear-when-task-reset] [--activity-exclude-from-recents]  
  35.         [--activity-launched-from-history] [--activity-multiple-task]  
  36.         [--activity-no-animation] [--activity-no-history]  
  37.         [--activity-no-user-action] [--activity-previous-is-top]  
  38.         [--activity-reorder-to-front] [--activity-reset-task-if-needed]  
  39.         [--activity-single-top]  
  40.         [--receiver-registered-only] [--receiver-replace-pending]  
  41.         [<URI>]  
  42.   
  43. #  

adb shell am activity/service/broadcast -a ACTION -c CATEGORY -n NAME

 

 

1. 启动activity/service

在adb shell中,通过am命令行启动一个Activity程序:


从superuser源代码中摘录一段使用示例:

am start -a android.intent.action.MAIN -n com.koushikdutta.superuser/com.koushikdutta.superuser.SuperuserRequestActivity --ei uid %d --ei pid %d

这个示例中:

-a 表示action (android.intent.action.MAIN)

-n 表示packagename (com.koushikdutta.superuser)

SuperuserRequestActivity是对应的Activity name

对应于AndroidManifest.xml文件中的描述。

 

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:versionCode="4" android:versionName="1.0.3" package="com.koushikdutta.superuser">  
  4.     <application android:icon="@drawable/icon" android:label="@string/app_name"  
  5.         android:debuggable="true">  
  6.         <activity android:name=".SuperuserActivity" android:label="Superuser Whitelist">  
  7.             <intent-filter>  
  8.                 <action android:name="android.intent.action.MAIN" />  
  9.                 <category android:name="android.intent.category.LAUNCHER" />  
  10.             </intent-filter>  
  11.         </activity>  
  12.         <activity android:name=".SuperuserRequestActivity">  
  13.             <intent-filter>  
  14.                 <action android:name="android.intent.action.MAIN" />  
  15.                 <category android:name="android.intent.category.DEFAULT" />  
  16.             </intent-filter>  
  17.         </activity>  
  18.     </application>  
  19. </manifest>   


 

2.发送broadcast,比如在模拟器中发生关机的broadcast

F:\>adb shell am broadcast -a android.intent.action.ACTION_SHUTDOWN -c android.intent.category.HOME
-n com.andy.androidtest/.ShutdownBroadcastReceiver

结果:
Broadcasting: Intent { act=android.intent.action.ACTION_SHUTDOWN cat=[android.intent.category.HOME]
cmp=com.andy.androidtest/.ShutdownBroadcastReceiver }
Broadcast completed: result=0

相关源代码:

ShutdownBroadcastReceiver.java

 

[java] view plaincopy
 
  1. public class ShutdownBroadcastReceiver extends BroadcastReceiver {  
  2.   
  3.     private static final String TAG = "ShutdownBroadcastReceiver";  
  4.     public ShutdownBroadcastReceiver()  
  5.     {  
  6.           
  7.     }  
  8.     //Once boot completed,start server  
  9.     public void onReceive(Context context, Intent intent)  
  10.     {  
  11.         String action = intent.getAction();  
  12.         if (action.equals(Intent.ACTION_SHUTDOWN))  
  13.         {  
  14.             //Toast.makeText(context, "Ready to shutdown....", 1000);             
  15.             Log.v(TAG,"action:"+action);  
  16.         }  
  17.     }  
  18. }  


AndroidManifest.xml:

 

 

 

[html] view plaincopy
 
  1. <receiver android:name=".ShutdownBroadcastReceiver" >  
  2.       <intent-filter>  
  3.           <action android:name="android.intent.action.ACTIOIN_SHUTDOWN" />  
  4.           <action android:name="android.intent.action.QUICKBOOT_POWEROFF" />                 
  5.       </intent-filter>  
  6.   </receiver>  


 Logcat将抓到action:

 

 

10-26 02:37:01.684: V/ShutdownBroadcastReceiver(293): action:android.intent.action.ACTION_SHUTDOWN

 

posted on 2014-08-07 14:05  Neal.Xu  阅读(5679)  评论(0编辑  收藏  举报

导航