安卓开发-BroadcastReceiver用法

在Android系统中,广播(Broadcast)是在组件之间传播数据(Intent)的一种机制。

Braodcast Receiver顾名思义就是广播接收器,它和事件处理机制类似,但是事件处理机制是程序组件级别的(比如:按钮的单击事件),而广播事件处理机制是系统级别的。我们可以用Intent来启动一个组件,也可以用sendBroadcast()方法发起一个系统级别的事件广播来传递消息。我们同样可以在自己的应用程序中实现Broadcast Receiver来监听和响应广播的Intent。

事件的广播通过创建Intent对象并调用sendBroadcast()方法将广播发出。事件的接受是通过定义一个继承BroadcastReceiver的类来实现的,继承该类后覆盖其onReceive()方法,在该方法中响应事件。

-------------------------------------------------

下面是android系统中定义了很多标准的Broadcast Action来响应系统的广播事件。

①ACTION_TIME_CHANGED(时间改变时触发)

②ACTION_BOOT_COMPLETED(系统启动完成后触发)--比如有些程序开机后启动就是用这种方式来实现的

③ACTION_PACKAGE_ADDED(添加包时触发)

④ACTION_BATTERY_CHANGED(电量低时触发)

------------------------------------------------

下面看一个例子:

我们在一个按钮上绑定一个事件,事件通过发送一个广播来触发logcat打出一个log。

先看manifest文件。

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.broadcast.BroadcastReceiverActivity"
            android:label="@string/app_name_bc" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" >
                </action>

                <category android:name="android.intent.category.LAUNCHER" >
                </category>
            </intent-filter>
        </activity>
        <receiver android:name="com.example.broadcast.HelloBroadReciever" >
           
            <intent-filter>
                <action android:name="comz.test.printlog" >
                </action>
            </intent-filter>
        </receiver>
    </application>

</manifest>
上面声明了一个receiver。接收名字是comz.test.printlog的消息。

看activity:

package com.example.broadcast;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.test.R;

public class BroadcastReceiverActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Button b1 = (Button) findViewById(R.id.broadcastBtn);

		b1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.e("mason", "here");
				// 定义一个intent
				Intent intent = new Intent().setAction("comz.test.printlog")
						.putExtra("info", "here is your info.");
				// 广播出去
				sendBroadcast(intent);
			}
		});
	}
}
在这段代码中,定义一个intent并发送广播出去。

看BroadReceiver的代码:

package com.example.broadcast;

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

public class HelloBroadReciever extends BroadcastReceiver {

	// 如果接收的事件发生
	@Override
	public void onReceive(Context context, Intent intent) {
		Log.e("mason", "on receive");
		if (intent.getAction().equals("comz.test.printlog")) {
			Log.e("mason", intent.getStringExtra("info"));
		}
	}
}
这是BroadcastReceiver的代码。

在接收到消息之后,如果消息是comz.test.printlog,则打印消息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值