来自http://blog.csdn.net/xlyrh/article/details/26072191,如有侵犯版权请通知我删除,谢谢!
am broadcast -a 过滤action -e/--es.........
参数说明如下:
<INTENT> specifications include these flags and arguments:
[-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> ...]
[--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
[--el <EXTRA_KEY> <EXTRA_LONG_VALUE> ...]
[--ef <EXTRA_KEY> <EXTRA_FLOAT_VALUE> ...]
[--eu <EXTRA_KEY> <EXTRA_URI_VALUE> ...]
[--ecn <EXTRA_KEY> <EXTRA_COMPONENT_NAME_VALUE>]
[--eia <EXTRA_KEY> <EXTRA_INT_VALUE>[,<EXTRA_INT_VALUE...]]
[--ela <EXTRA_KEY> <EXTRA_LONG_VALUE>[,<EXTRA_LONG_VALUE...]]
[--efa <EXTRA_KEY> <EXTRA_FLOAT_VALUE>[,<EXTRA_FLOAT_VALUE...]]
[-n <COMPONENT>] [-f <FLAGS>]
[--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]
[<URI> | <PACKAGE> | <COMPONENT>]
说明:
-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:
</activity>
<receiver android:name=".MyReceiver" >
<intent-filter>
<action android:name="com.example.mybroadcast.MainActivity" > ——————这里的action就是我们am broadcast -a +然后填写的内容act,
</action>
</intent-filter>
</receiver>
</application>