Andriod ADB开启Activity、Service以及BroadCast(包括参数的传递)
/*****************开启Activity 并传递参数*******************/
使用am命令启动Activity并传递参数的方法,也能用作C层与Java进行数据传递的一种手段。比如,我们要启动的Acitvity所在的app是net.yurushao.demo,需要启动的是其中的ExampleActivity,并给他传递两个参数:1. pid 整数,值为102. 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 valueString 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跳 ...
随机推荐
- MMORPG大型游戏设计与开发(part2 of net)
网络第二部分的将要给大家描述的是网络代码方面的设计,从基础的代码讲起,了解详细的网络模块构架. 没有放出整个源代码,是因为其中还有许多不足的地方,不过想必大家应该也能猜想出这个项目源码的地址了.不过对 ...
- ZooKeeper开发手册中文翻译(转)
本文Github地址:https://github.com/sundiontheway/zookeeper-guide-cn 本文假设你已经具有一定分布式计算的基础知识.你将在第一部分看到以下内容: ...
- 第10章 同步设备I/O和异步设备I/O(1)_常见设备及CreateFile函数
10.1 打开和关闭设备 10.1.1 设备的定义——在Windows中可以与之进行通信的任何东西. (1)常见设备及用途 设备 用途 用来打开设备的函数 文件 永久存储任何数据 CreateFile ...
- Unity热门插件推荐
前言 Unite2015的笔记 ,本文所提到的扩展主要针对 mobile上使用. 文中资源在Asset Store描述的截图日期:2016-04-28 Mesh Baker https://www.a ...
- file文件的操作
<?php //echo readfile('D:\\123.txt');//读取文件内容第一种方式,返回字符串,但是带有长度 echo '<br>'; //var_dump(fil ...
- Zygote浅谈
Zygote是什么 操作系统中,进程实际上是文件到地址空间的映射像.进程将要运行时,由操作系统将其映射到地址空间,完成这项工作的事物本质也应是一个进程,我们称这个进程为孵化进程,那么这个进程怎么收到消 ...
- java多线程系类:基础篇:01基本概念:
这个系类的内容全部来源于http://www.cnblogs.com/skywang12345/p/3479024.html.特别在此声明!!! 本来想直接看那位作家的博客的,但还是复制过来. 多线程 ...
- Swagger 增加 DocumentFilter 隐藏不需要显示的接口
services.ConfigureSwaggerGen(options => { options.SingleApiVersion(new Info { Version = "v1& ...
- java之yield(),sleep(),wait()区别详解
1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchronized同步快,其他线程仍然不能访问共享数据.注意该方 ...
- 开源--豆瓣小组UWP,已上架应用商店
1.前言 豆瓣小组是我和我老婆都比较喜欢的豆瓣家族里面的一款产品.平时加入了一些小组,偶尔打开看下新鲜的帖子,可以打发一下无聊的时间. 豆瓣小组UWP是我前几周在家里开发的一款windows 10应用 ...