Android adb 下 am broadcast -a发送信息详解

 

am broadcast -a 过滤action -e/--es.........

参数说明如下:

specifications include these flags and arguments:
    [-a ] [-d ] [-t ]
    [-c [-c ] ...]
    [-e|--es ...]
    [--esn ...]
    [--ez ...]
    [--ei ...]
    [--el ...]
    [--ef ...]
    [--eu ...]
    [--ecn ]
    [--eia [,     [--ela [,     [--efa [,     [-n ] [-f ]
    [--grant-read-uri-permission] [--grant-write-uri-permission]
    [--debug-log-resolution] [--exclude-stopped-packages]
    [--include-stopped-packages]
    [--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] [--activity-clear-task]
    [--activity-task-on-home]
    [--receiver-registered-only] [--receiver-replace-pending]
    [--selector]
    [ | | ]

 

说明:

-e/--es:传递String类型

-ef:传递float类型

。。。。其余同上,不同类型不能交互使用

 

例子:

 

C:\Users\yangrh2>adb shell am broadcast -a com.example.mybroadcast.MainActivity
--ef "int" 1.5f
Broadcasting: Intent { act=com.example.mybroadcast.MainActivity (has extras) }
Broadcast completed: result=0

 

类文件:

 

package com.example.mybroadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

 public MyReceiver() {
  Log.e("test", "------------------------------");
 }

 @Override
 public void onReceive(Context arg0, Intent arg1) {
  Log.e("test", "--------------start broadcast---------------");
  // TODO Auto-generated method stub
  Toast.makeText(arg0,
    "接收到Intent的Action为:" + arg1.getAction() + "\n消息内容是:"+arg1.getExtras()+
    arg1.getStringExtra("string")+"+add:"+arg1.getFloatExtra("int", 1.0f), Toast.LENGTH_LONG).show();
 }
}

Manifest:

       

       
           
                                ——————这里的action就是我们am broadcast -a +然后填写的内容act,
               

           

       

   

 

 

源码已实现

 

你可能感兴趣的:(android)