/*****************开启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

/*****************开启服务*******************/

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. private static final String TAG = "ShutdownBroadcastReceiver";
  3. public ShutdownBroadcastReceiver()
  4. {
  5. }
  6. //Once boot completed,start server
  7. public void onReceive(Context context, Intent intent)
  8. {
  9. String action = intent.getAction();
  10. if (action.equals(Intent.ACTION_SHUTDOWN))
  11. {
  12. //Toast.makeText(context, "Ready to shutdown....", 1000);
  13. Log.v(TAG,"action:"+action);
  14. }
  15. }
  16. }

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

Andriod ADB开启Activity、Service以及BroadCast(包括参数的传递)的更多相关文章

  1. android音乐播放器(Service+ContentProvider+Broadcast+Activity四大组件完成)

    1.获取音乐 1-1:获取手机中的音乐(用ContentProvider内容提供者来完成): package com.firefly.util; import java.util.ArrayList; ...

  2. 只有一个Service或Broadcast Reciver的android应用

    Service是android四大组件中与Activity最相似的组件,都可以代表可执行的程序. Service与Activity的区别在于:   (1).Service一直在后台运行,没有用户界面. ...

  3. Activity, Service,Task, Process and Thread之间的关系

    Activity, Service,Task, Process and Thread之间到底是什么关系呢? 首先我们来看下Task的定义,Google是这样定义Task的:a task is what ...

  4. Android Activity/Service/Broadcaster三大组件之间互相调用

    我们研究两个问题,1.Service如何通过Broadcaster更改activity的一个TextView.(研究这个问题,考虑到Service从服务器端获得消息之后,将msg返回给activity ...

  5. adb开启不了解决方案

    原文地址: adb开启不了解决方案 - vaecer - 博客频道 - CSDN.NET http://blog.csdn.net/vaecer/article/details/45894643   ...

  6. Android平台的四大天王:Activity, Service, ContentProvider, BroadcastReceiver

    今天开始要自学android,刚看到百度知道上面这段话,觉得不错(不过已经是2011年8月的回答了): Android系统的手机的每一个你能看到的画面都是一个activity,它像是一个画布,随你在上 ...

  7. 为windows开启winrm service, 以便进行远程管理

    为windows开启winrm service, 以便进行远程管理   是windows 一种方便远程管理的服务:开启winrm service,便于在日常工作中,远程管理服务器,或通过脚本,同时管理 ...

  8. 37.Activity之间的转换以及数据的传递(Intent)学习

      Intent简介:                                                                                在一个Androi ...

  9. Android笔记(二十) Activity中的跳转和值传递

    我们知道,一个APP是由若干个Activity组成的,那么各个Acitivity中肯定需要进行跳转以及传递数值以保证App的运行,现总结一下多个Activity之间的跳转和值传递. 显式Intent跳 ...

随机推荐

  1. 第5章 绘图基础_5.1-5.4 GDI绘图

    5.1 GDI的原理和结构 (1)提供一种特殊机制彻底隔离应用程序与不同输出设备(eg.显示器或打印机),以便支持 与设备无关的图形. 光栅设备(如显示器.激光打印机):图像是由点构成的矩阵 图形输出 ...

  2. 能跨域和跨浏览器的flashcookie for jquery插件

    对于写网站时需要跨域和跨浏览器的可以看看这个. 引入jquery  和 swfstore.min.js 就可以了,蛮简单好用的,会jquery基础就可以咯. mySwfStore.set('myKey ...

  3. guava

    原文出处: 黄博文 如果我要新建一个java的项目,那么有两个类库是必备的,一个是junit,另一个是Guava.选择junit,因为我喜欢TDD,喜欢自动化测试.而是用Guava,是因为我喜欢简洁的 ...

  4. [No000064]python 变量命名规范

    python源码和其他一些书籍,命名各种个性,没有一个比较统一的命名规范.于是总结了一些,供参考. 模块名: 模块应该使用尽可能短的.全小写命名,可以在模块命名时使用下划线以增强可读性.同样包的命名也 ...

  5. tshark过滤并保存包特定字段

    1.过滤端口为5001的tcp包,将时间输出 tshark -r h1.pcap -Y "tcp.port==5001" -T fields -e frame.time 时间格式如 ...

  6. Window 对象

    Window 对象 Window 对象表示浏览器中打开的窗口. 如果文档包含框架(<frame> 或 <iframe> 标签),浏览器会为 HTML 文档创建一个 window ...

  7. BZOJ4197[NOI2005]寿司晚宴

    Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴. 在晚宴上,主办方为大家提供了 n−1 种不同 ...

  8. 如何在Eclipse和Tomcat的Debug过程中启用热部署

    参考的地址是 http://blog.redfin.com/devblog/2009/09/how_to_set_up_hot_code_replacement_with_tomcat_and_ecl ...

  9. 深入运用js

    1,eval()函数 这个函数是获取参数的字符串,并将其作为js来处理,所以这里就有可能有人用这个来搞破坏(比如注入JS脚本文件等),所以最好的是方法是尽量少用,或者可以用new function() ...

  10. AngularJS中的事件

    欢迎大家指导与讨论 : ) 前言  Angular的作用域在本质上是分层次的(有的住户在低层, 有的住户在高层), 它们可以通过父子关系很自然地进行沟通.但通常, 这种沟通是单向的(父->子的单 ...