Android笔记三十三.BroadcastReceiver使用
Posted jzssuanfa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android笔记三十三.BroadcastReceiver使用相关的知识,希望对你有一定的参考价值。
BroadcastReceiver本质上是一种全局监听器。用于监听系统全局的广播消息并接收指定的广播,因此它能够很方便地实现系统中不同组件之间的通信。例如以下为BroadcastReceiver知识点结构:
但缺点是接收者不能将处理结果传递给下一个接收者。而且无法终止Broadcast Intent的传播。
有序广播接收者能够终止广播的传播(通过调用abortBroadcast()方法)。广播的传播一旦终止,后面的接收者就无法接收到广播。另外,广播的接收者能够将数据传递给下一个接收者(通过setResultExtras(Bundle bundle)方法)。
实现一个继承于BroadcastReceiver基类的子类,并实现里面的onReceiver()方法;
- public class MyBroadcastReceiver extends BroadcastReceiver
- {
- public void onReceiver(Context context,Intent intent){
- }
- }
- <receiver android:name=".MyBroadcastReceiver">
- <intent-filter>
- <action
- android:name = "com.jiangdongguo.android.myBroadcastReceover">
- <action/>
- </intent-filter>
- </receiver>
- MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver();
- IntentFilter filter = new IntentFilter("com.jiangdongguo.android.myBroadcastReceover"); //指定接收哪个广播
- registerReceiver(BroadcastReceiver receiver。IntentFilter filter)方法进行注冊
注冊完毕后。就可以接收相应的广播消息。一旦广播(Broadcast)事件发生后,系统就会创建相应的BroadcastRecevier实例,并自己主动触发它的onReceiver()方法,onReceiver()方法运行后,BroadcastReceiver的实例就会被销毁。
- package com.example.mybroadcast;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.widget.Toast;
- /*BroadcastReceiver子类
- * 用于接收指定发送广播*/
- public class MyBroadcastReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context arg0, Intent arg1) {
- Toast.makeText(arg0, "我是BroadcastReceiver,我已经接收到发送的广播。", Toast.LENGTH_SHORT).show();
- }
- }
- package com.example.mybroadcast;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- private Button sendBroad;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- /*1.广播接收器注冊方式二:注冊一个广播接收器*/
- MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver();
- IntentFilter filter = new IntentFilter("com.jiangdongguo.Android.BroadcastReceiver");
- registerReceiver(myBroadcastReceiver,filter);
- sendBroad = (Button)findViewById(R.id.send);
- sendBroad.setOnClickListener(new OnClickListener(){
- public void onClick(View v) {
- /*2.发送一个广播,并指明其action属性*/
- //a.指明该广播的action属性
- Intent intent = new Intent("com.jiangdongguo.Android.BroadcastReceiver");
- //b.发送广播
- sendBroadcast(intent);
- }
- });
- }
- }
- <?xml version="1.0" encoding="utf-8"?
>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.mybroadcast"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="14" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <!--广播接收器注冊方式一: 接收的哪一个广播-->
- <!-- <receiver android:name=".MyBroadcastReceiver">
- <intent-filter >
- <action android:name="com.jiangdongguo.Android.BroadcastReceiver"/>
- </intent-filter>
- </receiver> -->
- </application>
- </manifest>
这样就可能导致BroadcastReceiver启动的子线程不能运行完毕。
以上是关于Android笔记三十三.BroadcastReceiver使用的主要内容,如果未能解决你的问题,请参考以下文章
Linux学习笔记(三十三)iptables备份firewalld
Android项目实战(三十三):AS下获取获取依赖三方的jar文件aar 转 jar