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

一、广播机制

    在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>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值