您的位置:首页 > 移动开发 > Android开发

android 广播机制Broadcast、BroadcastReceiver用法解读

2011-08-24 18:34 761 查看
一、广播机制

在android系统中,发生的事情是可以被广播出去的。这就好像是微博。我发布一条消息,没加我关注的就不能看见,而我只能看见我加关注的消息,但是无论怎样消息都产生了。这就是广播机制。这个过程中一共有两个部分,即消息的发送和消息的接收。

二、收发消息的方式

1、发出消息。

有很多消息是系统消息,当事件产生就会在系统中发出,比如:当手机收到短息的时候,系统会产生 android.provider.Telephony.SMS_RECEIVED 消息,更多消息可以在文档Intent参数中找到。这种是消息是自动发出的。另一种情况就是添加自定义消息并发送。首先自定义Intent,对其中的Action做修改,也可以添加data。然后用sendBroadcast(intent)发出消息。

2、接收消息。

接收消息的方式只有一种,但是注册***——BroadcastReceiver的方式有两种。首先要先有个Receiver的类,它继承自BroadcastReceiver,重写OnReceive方法。有了类如何让其接收相应的消息呢?这就要注册***。
1、第一种注册***的方法是更改AndroidManifest.xml文件,添加:

<receiver android:name=".MyReceiver">

<intent-filter>

<action android:name="android.provider.Telephony.SMS_RECEIVED "></action>

</intent-filter>

</receiver>

2、第二种注册***的方法:

MyReceiver mr = new MyReceiver(); //定义一个Receiver

IntentFilter filter = new IntentFilter(); //定义一个过滤器

filter.addAction(SMS_ACTION); //设置过滤器过滤的动作

registerReceiver(mr,filter); //注册过滤器

三、举例:

接收短信消息。




ActivityMain.java

package com.fsy;

import android.app.Activity;

import android.content.Intent;

import android.content.IntentFilter;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class ActivityMain extends Activity {

/** Called when the activity is first created. */



public static String SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED";

public static String MY_ACTION = "com.fsy.gaga";

Button b1;

Button b2;

Button b3;



MyReceiver mr;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

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

b2 = (Button)findViewById(R.id.button2);

b3 = (Button)findViewById(R.id.button3);



b1.setOnClickListener(new OnClickListener(){

@Override

public void onClick(View v) {

mr = new MyReceiver();

IntentFilter filter = new IntentFilter();

filter.addAction(SMS_ACTION);

ActivityMain.this.registerReceiver(mr,filter);

Log.i("gaga", "filter is bounded");

}



});

b2.setOnClickListener(new OnClickListener(){

@Override

public void onClick(View v) {

unregisterReceiver(mr);

}



});



b3.setOnClickListener(new OnClickListener(){

@Override

public void onClick(View v) {

Intent t = new Intent(MY_ACTION);

sendBroadcast(t);

Log.i("gaga", "MY Mgs is sent");

}





});

}

}


MyReceiver.java

package com.fsy;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.util.Log;

public class MyReceiver extends BroadcastReceiver{



public MyReceiver(){

Log.i("gaga", "Created the MyReceiver!");

}

@Override

public void onReceive(Context context, Intent arg1) {

Log.i("gaga", "OnReceive is called!");

}

}



Manifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.fsy"

android:versionCode="1"

android:versionName="1.0">

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<uses-permission android:name="android.permission.RECEIVE_SMS" /> ....................配置这两条才响应短信广播

<application android:icon="@drawable/icon" android:label="@string/app_name">





<activity android:name=".ActivityMain"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: