Android發送廣播訊息
發送某個廣播Action最簡單的程式碼,需要在Activity下操作
public void broadcastAction()
{
//Log.d(TAG, "broadcastAction");
Intent intent = new Intent();
intent.setAction("com.example.SendBroadcast");
sendBroadcast(intent);
}
除了能夠透過寫APP程式發送外也可以用android adb shell的指令發送
Broadcasting: Intent { act=com.example.SendBroadcast }
Broadcast completed: result=0
action可以自己定義,或者用Android已定義在文件中以ACTION_開頭的常數
http://developer.android.com/intl/zh-tw/reference/android/content/Intent.html
Broadcasting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] pkg=com.example.SendBroadcast }
Broadcast completed: result=0
加上要傳送的Action就變成下面這樣(不建議用boot completed測試,有些Android裝置會重啟)
# am broadcast -a android.intent.action.BOOT_COMPLETED -p com.example.SendBroadcast
由於-p可省略就變成
# am broadcast -a android.intent.action.BOOT_COMPLETED com.example.SendBroadcast
Broadcasting: Intent { act=com.android.test (has extras) }
Broadcast completed: result=0
額外資料使用key和value的方式
# am broadcast -a com.example.SendBroadcast -e from tw.yahoo.com
Broadcasting: Intent { act=com.example.SendBroadcast (has extras) }
Broadcast completed: result=0
我們可以在 onCreate 註冊 broadcast receiver測試是否有效:
registerReceiver(new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
if(bundle != null && bundle.containsKey("from"))
{
String url = bundle.getString("from");
Log.d(TAG, "from:"+url);
}
}
}, new IntentFilter("com.example.SendBroadcast"));
# am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME
CATEGORY的種類在文件有提到, 都是以CATEGORY_開頭的常數
http://developer.android.com/intl/zh-tw/reference/android/content/Intent.html
參考資料:
Am命令用法 http://gityuan.com/2016/02/27/am-command/
Android 之Activity啟動模式(二)之 Intent的Flag屬性
public void broadcastAction()
{
//Log.d(TAG, "broadcastAction");
Intent intent = new Intent();
intent.setAction("com.example.SendBroadcast");
sendBroadcast(intent);
}
除了能夠透過寫APP程式發送外也可以用android adb shell的指令發送
送給所有接收Action
# am broadcast -a com.example.SendBroadcastBroadcasting: Intent { act=com.example.SendBroadcast }
Broadcast completed: result=0
action可以自己定義,或者用Android已定義在文件中以ACTION_開頭的常數
http://developer.android.com/intl/zh-tw/reference/android/content/Intent.html
送給特定Package
# am broadcast com.example.SendBroadcastBroadcasting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] pkg=com.example.SendBroadcast }
Broadcast completed: result=0
加上要傳送的Action就變成下面這樣(不建議用boot completed測試,有些Android裝置會重啟)
# am broadcast -a android.intent.action.BOOT_COMPLETED -p com.example.SendBroadcast
由於-p可省略就變成
# am broadcast -a android.intent.action.BOOT_COMPLETED com.example.SendBroadcast
加上額外資料
# am broadcast -a com.android.test --es test_string "this is test string" --ei test_int 100 --ez test_boolean trueBroadcasting: Intent { act=com.android.test (has extras) }
Broadcast completed: result=0
額外資料使用key和value的方式
# am broadcast -a com.example.SendBroadcast -e from tw.yahoo.com
Broadcasting: Intent { act=com.example.SendBroadcast (has extras) }
Broadcast completed: result=0
我們可以在 onCreate 註冊 broadcast receiver測試是否有效:
registerReceiver(new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
if(bundle != null && bundle.containsKey("from"))
{
String url = bundle.getString("from");
Log.d(TAG, "from:"+url);
}
}
}, new IntentFilter("com.example.SendBroadcast"));
設定發送類別
送給接收類別為home# am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME
CATEGORY的種類在文件有提到, 都是以CATEGORY_開頭的常數
http://developer.android.com/intl/zh-tw/reference/android/content/Intent.html
Am命令用法 http://gityuan.com/2016/02/27/am-command/
Android 之Activity啟動模式(二)之 Intent的Flag屬性
留言