Andriod ADB开启Activity、Service以及BroadCast(包括参数的传递)
来源:互联网 发布:tdee计算器软件 编辑:程序博客网 时间:2024/06/03 19:14
/*****************开启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
- Andriod ADB开启Activity、Service以及BroadCast(包括参数的传递)
- Andriod ADB开启Activity、Service以及BroadCast(包括参数的传递) .
- adb 启动Activity、Service,发送Broadcast以及Android常用的adb指令
- adb启动activity、service、Broadcast
- android am 启动activity service or broadcast及参数传递
- android am 启动activity service or broadcast及参数传递
- Android:adb 启动activity、service,发送broadcast的方法
- adb 启动Activity,service和发送broadcast
- Android:adb 启动activity、service,发送broadcast
- Android:adb 启动activity、service,发送broadcast
- adb启动activity、service或broadcast
- Android:adb 启动activity、service,发送broadcast
- Android:adb 启动activity、service,发送broadcast
- Android:adb 启动activity、service,发送broadcast
- Android:adb 启动activity、service,发送broadcast
- adb命令启动activity、service,发送broadcast
- Android:adb shell am命令行发送Activity/Service/Broadcast
- Android:adb shell am命令行发送Activity/Service/Broadcast
- Eclipse Problems中提示没有build问题
- Keras——环境配置
- SpringMVC ExceptionHandler 可以传递的参数及返回值
- 剑指offer 29 最小的k个数
- java 缓冲流的操作
- Andriod ADB开启Activity、Service以及BroadCast(包括参数的传递)
- loadView、viewDidLoad、initWithCoder、initWithNibName、awakeFromNib的用法
- 杂
- struts2文件上传
- Swing批量重命名并导出excel
- 集群 和 分布式
- C++库汇总
- 使用workflow客制出货通知单
- 如何自学Android编程