首页 > 其他分享 >Andriod ADB开启Activity、Service以及BroadCast(包括参数的传递)

Andriod ADB开启Activity、Service以及BroadCast(包括参数的传递)

时间:2022-12-16 12:07:21浏览次数:52  
标签:Service -- am BroadCast ADB action intent android com


/*****************开启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文件中的描述。

 




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





1. public class ShutdownBroadcastReceiver extends BroadcastReceiver {  
2.
3. private static final String TAG = "ShutdownBroadcastReceiver";
4. public ShutdownBroadcastReceiver()
5. {
6.
7. }
8. //Once boot completed,start server
9. public void onReceive(Context context, Intent intent)
10. {
11. String action = intent.getAction();
12. if (action.equals(Intent.ACTION_SHUTDOWN))
13. {
14. //Toast.makeText(context, "Ready to shutdown....", 1000);
15. "action:"+action);
16. }
17. }
18. }



AndroidManifest.xml:






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



标签:Service,--,am,BroadCast,ADB,action,intent,android,com
From: https://blog.51cto.com/u_7548043/5947084

相关文章

  • 【221215-5】已知:D是三角形ABC外一点,连接AD,BD,CD,角BAC=角BDC=α。求证:角ADB=角BDC(大连
    以上提供了两种解法,第一解法要学圆以后才能用,第二解法正好适用于八年级。......
  • service.Close() 问题
    我在用WCF做邮件服务的时候,客户端在发送完命令后,需要加service.Close();不然在连续10次发送信息到WCF服务器,滴10次以后就发不出去了。MyEmailSend.EmailSendServiceClie......
  • adb操作WiFi
    目录adb设置安卓连接wifi一.修改wpa_supplicant.conf文件二.重启wifi三,adb命令让手机不进入休眠adb设置安卓连接wifi一.修改wpa_supplicant.conf文件1.1.获得roo......
  • adb按键命令
    android用adb命令唤醒和熄灭屏幕//发一次熄灭屏幕,再发一次点亮屏幕。可以在板级没有接出power键时使用。adbshellinputkeyevent26//power事件滑动adbshellin......
  • 简单创建webservice接口并采用rpc方式调用
    简单创建webservice接口并采用rpc方式调用一、简单创建webservice接口1.新建一个maven项目2.创建一个包3.创建一个接口在新建的包下创建接口点击查看代码public......
  • 从Mariadb迁移到postgresql
    前言本文主要实验所用的环境,后端是golang,ginweb框架.mysqldriver:github.com/go-sql-driver/mysqlv1.4.1迁移后PostgreSQLdriver:github.com/jackc/pgx/v5v......
  • mybatis-plus的通用Service
    mybatis-plus的通用service,实际上进一步封装了CRUD操作,同时方法的命名区分BaseMapper,也就是可以通过继承通用service,就可以使用一些基本的CRUD操作了。如何使用内在......
  • 关于在springboot中的controller引入不了service报错解决办法
    关于在springboot中的controller引入不了service报错解决办法报错如下:不仅是controller一层出现问题,每一层都是相同的问题解决步骤:1.当出现了这个错误不要慌,首先清......
  • k8s--service 之 HeadLiness、NodePort 使用
    前戏环境还是使用我们上节的环境HeadLiness在某些场景中,开发人员可能不想使用Service提供的负载均衡功能,而希望自己来控制负载均衡策略,针对这种情况,kubernetes 提供了H......
  • 019.开发请假申请Service层(下)
    1.创建请假单服务(com.imooc.oa.service)LeaveFormService.javapackagecom.imooc.oa.service;importcom.imooc.oa.entity.Employee;importcom.imooc.oa.entity.Leave......