/*****************開啓Activity 並傳遞參數*******************/
使用am命令啓動Activity並傳遞參數的方法,也能用作C層與Java進行數據傳遞的一種手段。
比如,我們要啓動的Acitvity所在的app是net.yurushao.demo,需要啓動的是其中的ExampleActivity,並給他傳遞兩個參數:
1. pid 整數,值爲10
2. str 字符串,"hello, world"
那麼,完整的命令爲(在Android Shell中執行):
am start -a android.intent.action.MAIN -n --ei pid 10 --es str "hello, world"
net.yurushao.demo/net.yurushao.demo.ExampleActivity
簡單說明一下,--ei表示參數類型爲整型(extra integer),--es表示參數的類型爲字符串(extra string),然後它們後面分別跟一個鍵值對,標識參數名和具體值。需要其他類型可以參考開頭提到的那篇文章或者使用 am -h 查看幫助。
在ExampleActivity中獲取傳遞來的參數也非常簡單,在onCreate回調函數中添加:
[java]
Intent intent = getIntent();
int pid = intent.getIntExtra("pid", -1); // 第二個參數爲default value
String str = intent.getStringExtra("str");
[/java]
然後在AndroidManifest.xml中表示ExampleActivity的標籤下,添加並接受android.intent.action.MAIN
/*****************開啓服務*******************/
傳遞參數:
/***************停止服務*****************/
//嘗試過有效
//嘗試過無效
/******************發送廣播*****************/
示例一:
adb shell am broadcast 後面的參數有:
[-a <ACTION>]
[-d <DATA_URI>]
[-t <MIME_TYPE>]
[-c <CATEGORY> [-c <CATEGORY>] ...]
[-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
[--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
[-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
[-n <COMPONENT>]
[-f <FLAGS>] [<URI>]
例如:
adb shell am broadcast -a com.android.test --es test_string "this is test string" --ei test_int 100 --ez test_boolean true
實例二
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
http://blog.csdn.net/yueludanfeng/article/details/51130857