Android广播机制详解

Broadcast Receiver用于接收并处理广播通知(broadcastannouncements)。

多数的广播是系统发起的,如地域变换、电量不足、来电来信等。

程序可以有任意数量的Broadcast Receivers来响应它觉得重要的通知。

Broadcast Receiver可以通过多种方式通知用户:启动activity、使用NotificationManager、开启背景灯、振动设备、播放声音等,最典型的是在状态栏显示一个图标,这样用户就可以点它打开看通知内容。

通常我们的某个应用或系统本身在某些事件(电池电量不足、来电来短信)来临时会广播一个Intent出去,我们可以利用注册一个Broadcast Receiver来监听到这些Intent并获取Intent中的数据。

一、在程序中接受系统发的广播

我们举一个例子说明,一个接受系统Date改变的广播的例子。

先新建一个HelloBroadcastReceiver.java类,继承自BroadcastReceiver并复写它的onReceive方法。这样就创建了一个专门用来接收广播的类。

package com.tianjf;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class HelloBroadcastReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.d("debug", "Date has been changed!");        
		Log.d("debug", ""+intent.getAction()); 
	}
}

但是上面的Broadcast Receiver没有指定接收哪个广播,我们要指定某个Receiver接收哪个广播,必须在AndroidManifest.xml中注册此Receiver

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tianjf"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".BroadcastDemoActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- 定义Broadcast Receiver指定监听的Action(系统启动action) -->
        <receiver android:name="HelloBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.DATE_CHANGED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

OK,运行之后,更改系统Date看看,果然打印了两行Log。(着两行Log有时候打得出来,有时候打不出来,不知道神马原因)

二、接收自己发的广播

直接上代码

BroadcastDemoActivity.java

package com.tianjf;

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 BroadcastDemoActivity extends Activity implements OnClickListener {

	private Button button;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		button = (Button) findViewById(R.id.button);
		button.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// 定义一个intent
		Intent intent = new Intent();
		intent.setAction("MY_BROADCAST_ACTION");
		intent.putExtra("yaoyao",
				"yaoyao is 189 days old ,27 weeks -- 2010-08-10");
		// 广播出去
		sendBroadcast(intent);
	}
}

HelloBroadcastReceiver.java

package com.tianjf;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class HelloBroadcastReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.d("debug", "" + intent.getAction());
		if (intent.getAction().equals("android.intent.action.DATE_CHANGED")) {
			Log.d("debug", "Date has been changed!");
			Log.d("debug", "" + intent.getAction());
		} else if (intent.getAction().equals("MY_BROADCAST_ACTION")) {
			Log.d("debug", "Say Hello to Yaoyao!");
			Log.d("debug", intent.getStringExtra("yaoyao"));
		}
	}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tianjf"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".BroadcastDemoActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- 定义Broadcast Receiver指定监听的Action(系统启动action和自动义的广播) -->
        <receiver android:name="HelloBroadcastReceiver" >
            <intent-filter>
                <action android:name="MY_BROADCAST_ACTION" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.DATE_CHANGED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值