Android(安卓)app接收来自adb发送的广播的实例学习
16lz
2021-01-26
Android app接收来自adb发送的广播的实例学习
简单介绍:通过命令行执行adb shell am broadcast发送广播通知。adb shell am broadcast 后面的参数有:
[-a ]
[-d
[-t
[-c
[-e|--es
[--ez
[-e|--ei
[-n
[-f
例如:
adb shell am broadcast -a com.android.test --es test_string "this is test string" --ei test_int 100 --ez test_boolean true
说明:蓝色为key,红色为alue,分别为String类型,int类型,boolean类型
一、app 主要代码 1、MainActivity.java
package com.example.receiverbroadcastsendbyadb;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView;public class MainActivity extends Activity {private TextView Recevier_Text; private final BroadcastReceiver mReceiverBroadCast = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String msg = intent.getStringExtra("msg"); Recevier_Text.setText("great!Received a Broadcast! "+"msg="+msg); } };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.BROADCAST_FORM_ADB");Recevier_Text=(TextView)findViewById(R.id.textView1);Recevier_Text.setText("wait a Broadcast.....");registerReceiver(mReceiverBroadCast, filter);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}
2、AndroidManifest.xml代码
二、执行 1、运行app,中间的文本显示等待接收adb发过来的广播。
2、执行adb shell am broadcast -a android.intent.action.BROADCAST_FORM_ADB --es msg "i love tanghanyue"
3、执行上面的命令后:
4、测试结果接收到adb发过来的广播。
三,当然可以发送系统广播等,例如亮屏广播adb shell am broadcast -a Intent.ACTION_SCREEN_ON,然后在接收端接收这个广播就可以,测试ok。
更多相关文章
- Android定制之Ubuntu下常见问题及解决
- android下的异步任务
- Android(安卓)Action的常用
- Android的跨进程通信
- android NDK开发中,用Cygwin调试本地代码时报错“Another debug s
- Android监听收到的短信
- Android面试基础(三)——四大组件的介绍
- android 横竖屏切换生命周期详解
- Android的Activity的小知识点