Andriod ADB開啓Activity、Service以及BroadCast(包括參數的傳遞) .

 /*****************開啓Activity  並傳遞參數*******************/

使用am命令啓動Activity並傳遞參數的方法,也能用作C層與Java進行數據傳遞的一種手段。
 
比如,我們要啓動的Acitvity所在的appnet.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

 /*****************開啓服務*******************/

am startservice com.example.serviceoftestethernet/com.example.lxm.Myservice
或者
am startservice --user 0 -n com.example.serviceoftestethernet/com.example.lxm.Myservice

傳遞參數:

am startservice --es cmd "startService" --ei inttest 10 com.example.serviceoftestethernet/com.example.lxm.Myservice
注意:參數不能放在最後面,而是要放在包名前面(啓動Activity與Service都是這樣的,如果放在最後面是不能被識別的)


 /***************停止服務*****************/

 //嘗試過有效

am force-stop  com.example.serviceoftestethernet  


 //嘗試過無效

am stopservice --user 0  com.example.serviceoftestethernet

/******************發送廣播*****************/

am broadcast -a  com.example.lxm.static_message 



示例一:

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

實例二

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 plain copy
  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 plain copy
  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 plain copy
  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



http://blog.csdn.net/yueludanfeng/article/details/51130857


發佈了5 篇原創文章 · 獲贊 12 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章