Android学习之BroadcastReceiver

来源:互联网 发布:mac系统怎么远程控制 编辑:程序博客网 时间:2024/05/16 17:08
BroadcastReceiver是Android系统引入的一种消息广播机制,它广泛应用于应用程序之间传输信息。通常,在系统运行过程中,后台会发起大量的广播通知,如地域变换、电量不足、来电来短信来应用消息提醒等。
BroadcastReceiver就相当于收音机,而各种各样的广播电台就是消息的发送者。电台通过特定的频率发送内容,收音机只有将接受频率调成和广播电台一样的频率便可以收听相应电台的内容了。
在Android系统中,发送和接受的工作分别由sendBroadcast方法和注册的BroadcastReceiver类完成。并且只有当发送和接受具有相同频率时,接受者才能收到发送者发出的消息,这里的频率就是Intenr对象的action属性。

示例:
单击按钮时,发送广播,注册两个广播接收器(创建BroadcastReceiver类的两个对象,并在AndroidManifest.xml中注册)。当接收器与广播的action相匹配时,则会执行该接收器中的onReceive方法,在控制台打印广播信息。
一共三个java文件:MainActivity.java, InValidateReceiver.java, ValidateReceiver.java
1. MainActivity.java ( src/com.example.broadcast.MainActivity.java)
package com.example.broadcast;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {    private Button btn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btn = (Button)findViewById(R.id.btn);        btn.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View arg0) {                //1.create new intent.                Intent intent = new Intent();                //2.set broadcast content.                intent.putExtra("CONTENT", "This is broadcast demo.");                //3.set broadcast action                intent.setAction("com.example.broadcast.validate");                //4.send broadcast                sendBroadcast(intent);            }        });    }}

2. InValidateReceiver.java ( src/com.example.broadcast.InValidateReceiver.java)
package com.example.broadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class InValidateReceiver extends BroadcastReceiver{    @Override    public void onReceive(Context context, Intent intent) {        Log.v("InValidateReceiver", intent.getStringExtra("CONTENT"));    }}

3. ValidateReceiver.java ( src/com.example.broadcast.ValidateReceiver.java)
package com.example.broadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class ValidateReceiver extends BroadcastReceiver{    @Override    public void onReceive(Context context, Intent intent) {        Log.v("ValidateReceiver", intent.getStringExtra("CONTENT"));    }}



4. activity_main.xml -->MainActivity的布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <Button                    //创建一个按钮        android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="btn"        /></LinearLayout>




5. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.broadcast"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="14"        android:targetSdkVersion="21" />    <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/>注册BroadcastReceiver    <receiver android:name=".ValidateReceiver" >        <intent-filter >            <action android:name="com.example.broadcast.validate"/>        </intent-filter>    </receiver>    <receiver android:name=".InValidateReceiver" >        <intent-filter >            <action android:name="com.example.broadcast.invalidate"/>        </intent-filter>    </receiver>    </application></manifest>

从结构上可知接收器只接收与自己有相同的action的广播,所以这里ValidateReceiver接收广播信息,而不是InValidateReceive。




1 0
原创粉丝点击