Andriod ADB开启Activity、Service以及BroadCast(包括参数的传递)
/*****************开启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(包括参数的传递)的更多相关文章
- android音乐播放器(Service+ContentProvider+Broadcast+Activity四大组件完成)
1.获取音乐 1-1:获取手机中的音乐(用ContentProvider内容提供者来完成): package com.firefly.util; import java.util.ArrayList; ...
- 只有一个Service或Broadcast Reciver的android应用
Service是android四大组件中与Activity最相似的组件,都可以代表可执行的程序. Service与Activity的区别在于: (1).Service一直在后台运行,没有用户界面. ...
- Activity, Service,Task, Process and Thread之间的关系
Activity, Service,Task, Process and Thread之间到底是什么关系呢? 首先我们来看下Task的定义,Google是这样定义Task的:a task is what ...
- Android Activity/Service/Broadcaster三大组件之间互相调用
我们研究两个问题,1.Service如何通过Broadcaster更改activity的一个TextView.(研究这个问题,考虑到Service从服务器端获得消息之后,将msg返回给activity ...
- adb开启不了解决方案
原文地址: adb开启不了解决方案 - vaecer - 博客频道 - CSDN.NET http://blog.csdn.net/vaecer/article/details/45894643 ...
- Android平台的四大天王:Activity, Service, ContentProvider, BroadcastReceiver
今天开始要自学android,刚看到百度知道上面这段话,觉得不错(不过已经是2011年8月的回答了): Android系统的手机的每一个你能看到的画面都是一个activity,它像是一个画布,随你在上 ...
- 为windows开启winrm service, 以便进行远程管理
为windows开启winrm service, 以便进行远程管理 是windows 一种方便远程管理的服务:开启winrm service,便于在日常工作中,远程管理服务器,或通过脚本,同时管理 ...
- 37.Activity之间的转换以及数据的传递(Intent)学习
Intent简介: 在一个Androi ...
- Android笔记(二十) Activity中的跳转和值传递
我们知道,一个APP是由若干个Activity组成的,那么各个Acitivity中肯定需要进行跳转以及传递数值以保证App的运行,现总结一下多个Activity之间的跳转和值传递. 显式Intent跳 ...
随机推荐
- [转]Backbone.js简单入门范例
本文转自:http://dmyz.org/archives/598 11年刚开始用前端MVC框架时写过一篇文章,当时Knockout和Backbone都在用,但之后的项目全是在用Backbone,主要 ...
- MMORPG大型游戏设计与开发(part3 of net)
这一部分需要向大家介绍的是服务器的select以及收发包的具体流程,从核心代码功能上分析网络交互具体过程. 首先大家要看第二部分(part2 of net)的代码结构图,因为在接下来的流程过程中会用到 ...
- 【HTML5】使用多媒体
HTML5 支持直接在浏览器中播放音频和视频文件,不需要使用Abode Flash这样的插件. 1. 使用 video 元素 可以用video 元素在网页里嵌入视频内容. 其基本用法如下: <! ...
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
A 思路: 贪心,每次要么选两个最大的,要么选三个,因为一个数(除了1)都可以拆成2和3相加,直到所有的数都相同就停止,这时就可以得到答案了; C: 二分+bfs,二分答案,然后bfs找出距离小于等于 ...
- 【原创Android游戏】--猜数字游戏V1.1 --数据存储,Intent,SimpleAdapter的学习与应用
--------------------------------------------------------------- V0.1版本 上次做完第一个版本后,发现还有一些漏洞,并且还有一些可以添 ...
- Student管理系统
使用三层架构实现Student管理系统,分为Studrnt.Model层,Student.DAL层,Student.BLL层和Student.UI层 步骤分析: 1.在Student.Model添加S ...
- [No00001E]不出国,学口语-出国口语自然好?才怪咧!
- Linux下Mongodb安装和启动配置
1.下载安装包 wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.8.2.tgz 下载完成后解压缩压缩包 tar zxf mongod ...
- shell 删除某个目录下的重复文件
#!/bin/bash ls -lS | awk 'BEGIN{ getline; getline; name1=$;size=$; } { name2=$; sizeTmp=$; ){ ; ; if ...
- ASP.NET Repeater 绑定 DropDownList Calendar 选择日期
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...