自定义广播接收不到
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity" ; public static final String MY_ACTION = "com.imooc.demo.adcdefg" ; public static final String CONTENT = "broadcast_content" ; private ImoocBroadcastReceiver broadcastReceiver; private EditText editText; private Button btn; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); broadcastReceiver = new ImoocBroadcastReceiver(textView); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL); intentFilter.addDataScheme( "package" ); intentFilter.addAction(Intent.ACTION_SCREEN_ON); intentFilter.addAction(Intent.ACTION_SCREEN_OFF); //自定义一个广播,加入到接收器 intentFilter.addAction(MY_ACTION); registerReceiver(broadcastReceiver, intentFilter); Log.d(TAG, "设置监听器" ); setEvents(); } private void setEvents() { btn.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "点击按钮了" ); Intent intent= new Intent(MY_ACTION); intent.putExtra(CONTENT,editText.getText().toString()); sendBroadcast(intent); Log.d(TAG, "广播发送了" ); } }); } private void init() { editText = findViewById(R.id.et_broadcast_content); btn = findViewById(R.id.btn_send_broadcast); textView = findViewById(R.id.tv_received_broadcast); } @Override protected void onDestroy() { super .onDestroy(); if (broadcastReceiver != null ) { unregisterReceiver(broadcastReceiver); } } } |
ImoocBroadcastReceiver
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public class ImoocBroadcastReceiver extends BroadcastReceiver { TextView textView; public ImoocBroadcastReceiver() { } public ImoocBroadcastReceiver(TextView textView) { this .textView = textView; } private static final String TAG = "ImoocBroadcastReceiver" ; @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "触发接收器了" ); if (intent != null ) { Log.d(TAG, "进入接收器消息处理了" ); String action = intent.getAction(); Log.d(TAG, "广播接收器信息:" + action); if (TextUtils.equals(action, MainActivity.MY_ACTION)) { if (textView != null ) { String content = intent.getStringExtra(MainActivity.CONTENT); textView.setText( "接收到的action是:" + action + "\n接收到的内容是:\n" + content); } } } } } |
manifest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" package = "com.imooc.broadcastreceiverdemo" > < application android:allowBackup = "true" android:icon = "@mipmap/ic_launcher" android:label = "@string/app_name" android:roundIcon = "@mipmap/ic_launcher_round" android:supportsRtl = "true" android:theme = "@style/AppTheme" tools:ignore = "GoogleAppIndexingWarning" > < activity android:name = ".MainActivity" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > < receiver android:name = ".ImoocBroadcastReceiver" > <!--<intent-filter>--> <!--<!–应用安装 –>--> <!--<action android:name="android.intent.action.PACKAGE_INSTALL"/>--> <!--<!–应用卸载 –>--> <!--<action android:name="android.intent.action.PACKAGE_REMOVED"/>--> <!--<!–指定数据类型 –>--> <!--<data android:scheme="package"/>--> <!--<!–连接充电器 –>--> <!--<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>--> <!--<!–断开充电器 –>--> <!--<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>--> <!--<!–充电完成 –>--> <!--<action android:name="android.intent.action.BATTERY_OKAY"/>--> <!--<!–电量低 –>--> <!--<action android:name="android.intent.action.BATTERY_LOW"/>--> <!--<!–开机启动 –>--> <!--<action android:name="android.intent.action.BOOT_COMPLETED"/>--> <!--</intent-filter>--> </ receiver > </ application > </ manifest > |
3
收起
正在回答
3回答
1)你把addDataScheme()删除掉就可以收到自定义的广播了。如果加上的话,是要求广播中还要有DataScheme并且符合你的规则。2)但是例如删除APP这个系统广播,如果你不带addDataScheme()则会接收不到这个系统广播。3)要想两者都接收到,你就需要定义多个广播接收器了。祝:学习愉快
D许咚
2019-05-16 19:33:00
xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:app= "http://schemas.android.com/apk/res-auto" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= ".MainActivity" android:layout_marginLeft= "10dp" android:layout_marginRight= "10dp" > <TextView android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "请输入广播内容:" /> <EditText android:id= "@+id/et_broadcast_content" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:lines= "2" /> <Button android:id= "@+id/btn_send_broadcast" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:gravity= "center" android:text= "发送广播" /> <TextView android:id= "@+id/tv_received_broadcast" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "接收到的广播内容:" /> </LinearLayout> |
Android数据通信开发与应用2018版
- 参与学习 人
- 提交作业 147 份
- 解答问题 687 个
本专题介绍了Android开发核心组件:广播、服务及全局应用。教会你如何使用AIDL、Thread、Socket、蓝牙解决进程线程间通信问题。利用Glide等实现异步加载及NDK原生代码开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧