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

来源:互联网 发布:netty 监听多个端口 编辑:程序博客网 时间:2024/05/19 22:28

0.adb shell am命名行参数

F:\>adb shell# amamusage: am [subcommand] [options]    start an Activity: am start [-D] [-W] <INTENT>        -D: enable debugging        -W: wait for launch to complete    start a Service: am startservice <INTENT>    send a broadcast Intent: am broadcast <INTENT>    start an Instrumentation: am instrument [flags] <COMPONENT>        -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)        -e <NAME> <VALUE>: set argument <NAME> to <VALUE>        -p <FILE>: write profiling data to <FILE>        -w: wait for instrumentation to finish before returning    start profiling: am profile <PROCESS> start <FILE>    stop profiling: am profile <PROCESS> stop    <INTENT> specifications include these flags:        [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]        [-c <CATEGORY> [-c <CATEGORY>] ...]        [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]        [--esn <EXTRA_KEY> ...]        [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]        [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]        [-n <COMPONENT>] [-f <FLAGS>]        [--grant-read-uri-permission] [--grant-write-uri-permission]        [--debug-log-resolution]        [--activity-brought-to-front] [--activity-clear-top]        [--activity-clear-when-task-reset] [--activity-exclude-from-recents]        [--activity-launched-from-history] [--activity-multiple-task]        [--activity-no-animation] [--activity-no-history]        [--activity-no-user-action] [--activity-previous-is-top]        [--activity-reorder-to-front] [--activity-reset-task-if-needed]        [--activity-single-top]        [--receiver-registered-only] [--receiver-replace-pending]        [<URI>]#
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文件中的描述。

 

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"android:versionCode="4" android:versionName="1.0.3" package="com.koushikdutta.superuser"><application android:icon="@drawable/icon" android:label="@string/app_name"android:debuggable="true"><activity android:name=".SuperuserActivity" android:label="Superuser Whitelist"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".SuperuserRequestActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.DEFAULT" /></intent-filter></activity></application></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

public class ShutdownBroadcastReceiver extends BroadcastReceiver {private static final String TAG = "ShutdownBroadcastReceiver";    public ShutdownBroadcastReceiver()    {        }    //Once boot completed,start server    public void onReceive(Context context, Intent intent)    {String action = intent.getAction();if (action.equals(Intent.ACTION_SHUTDOWN)){//Toast.makeText(context, "Ready to shutdown....", 1000);Log.v(TAG,"action:"+action);}    }}

AndroidManifest.xml:

      <receiver android:name=".ShutdownBroadcastReceiver" >            <intent-filter>                <action android:name="android.intent.action.ACTIOIN_SHUTDOWN" />                <action android:name="android.intent.action.QUICKBOOT_POWEROFF" />                           </intent-filter>        </receiver>

 Logcat将抓到action:

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



原创粉丝点击