广播接收者BroadcastReceiver

时间:2023-03-09 02:43:41
广播接收者BroadcastReceiver
  BroadcastReceiver与activity,service有完整的生命周期不同,BroadcastReceiver本质上是一系统级别的监听器,专门负责监听各程序发出的broadcast.与程序级别的监听器不同的是,例如OnXxxListener(),这些监听器运行在指定程序进程中,当程序退出时,oNXxxListener也随之关闭。但BroadcastReceiver属于系统级别的监听器,拥有自己的进程,只要存在与之匹配的Intent被广播出来,BroadcastReceiver总会被激发。
  指定该BroadcastReceiver能匹配的Intent有两种方式:
  1.使用代码指定:
    调用BroadcastReceiver的context的registerReceiver(BroadcastReceiver recevier,IntentFilter filter).例如:
    IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
        IncomingSMSRecevier receiver = new IncomingSMSRecevier();
        registerReceiver(receiver, filter);
  2.在AndroidManifest.xml文件中配置
   <receiver android:name=".IncomingSMSRecevier ">
            <intent-filter >
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>
  注意:如果BroadcastReceiver方法不能在10秒内执行完成,Android会认为该程序无响应,不要在BroadcastReceiver的OnReceiver方法内执行一些耗时的操作,否则会弹出ANR(Application No Response)的对话框。
 例子,发送一个普通广播,接收广播的例子:
 step1: main.xml
 <?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="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Button android:id="@+id/send"
        android:text="点击发送广播"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

step2:创建发送广播的activity为BroadCastMain:
package com.lp.ecjtu;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class BroadCastMain extends Activity {
    private Button sendBtn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sendBtn = (Button) findViewById(R.id.send);
        sendBtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                //设置intent的Action属性
                intent.setAction("com.lp.action.WELCOME_BROADCAST");
                intent.putExtra("msg", "消息");
                //发送广播,使用intent发送一条广播
                sendBroadcast(intent);
            }
        });
    }
}

step3:创建接收广播的类MyReceiver.java
package com.lp.ecjtu;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/** 当符合该MyReceiver的广播出现时,onReceive方法会被触发,从而显示广播所携带的消息 */
public class MyReceiver extends BroadcastReceiver{

@Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(context, "接受到的Intent的Action是:"
                        +intent.getAction()
                        +"\n消息的内容为:"+intent.getStringExtra("msg")
                , Toast.LENGTH_LONG).show();
    }
    
}

step4:别忘了:在AndroidManifest.xml中增加:
<!-- 指定该BroadcastReceiver所响应的Intent的Action -->
        <receiver android:name=".MyReceiver">
            <intent-filter >
                <action android:name="com.lp.action.WELCOME_BROADCAST"/>
            </intent-filter>
        </receiver>
step5运行结果:
广播接收者BroadcastReceiver
广播又可以分为“普通广播”和“有序广播”,上面介绍的普通广播,下面介绍有序广播:
  普通广播和有序广播各自的优缺点:
  普通广播是完全异步的,可以在同一时刻(逻辑上)被所有接收者接收到,优点效率比较高,缺点接收者不能将处理结果传递给下一个接收者,并且无法终止广播Intent的传播。
  有序广播是按照接收者声明的优先级别,被接收者依次接收广播。如:A的级别高于B,B的级别高于C,那么,广播首先传给A,再传给B,最后传给 C 。优先级别在<intent-filter>的android:priority属性中声明,数值越大优先级别越高,取值范围:-1000到 1000,优先级别也可以调用IntentFilter对象的setPriority()进行设置 。
  有序广播的接收者可以终止广播Intent的传播,广播Intent的传播一旦终止,后面的接收者就无法接收到广播。另外,有序广播的接收者可以将数据传递给下一个接收者,如:A得到广播后,可以往它的结果对象中存入数据,当广播传给B时,B可以从A的结果对象中得到A存入的数据。
  好吧知道了有序广播的原理后,我们来看一个例子:
step1:main.xml
<?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="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Button android:id="@+id/send"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="点击发送有序广播"
        />
</LinearLayout>

step2 发送有序广播:
package com.lp.ecjtu;

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 BroadcastSorted extends Activity {
    private Button sendbtn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sendbtn = (Button) findViewById(R.id.send);
        sendbtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setAction("com.lp.action.WELCOME_BROADCAST");
                    intent.putExtra("msg", "消息");
                    //发送有序广播
                    sendOrderedBroadcast(intent, null);
            }
        });   
    }
}
step3 第一个BroadcastReceiver:
package com.lp.ecjtu;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver{

@Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(context, "接收到的intent的action为:"+intent.getAction()
                +"\nintent的消息的内容为:"+intent.getStringExtra("msg"), Toast.LENGTH_LONG).show();
        //创建一个Bundle对象
        Bundle mBundle = new Bundle();
        mBundle.putString("first", "第一个BroadcastReceiver存入的消息");
        //将Bundle放入结果中
        setResultExtras(mBundle);
        //取消broadcast的继续传播
        //abortBroadcast();
    }

}
step4第二个BroadcastReceiver

package com.lp.ecjtu;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MyReciever2 extends BroadcastReceiver{

@Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = getResultExtras(true);
        //解析前一个BroadcastReceiver所存入的key为first的消息
        String first = bundle.getString("first");
        Toast.makeText(context, "第一个Broadcast存入的消息为:"+first,500000).show();
    }

}
step5

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

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

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".BroadcastSorted"
            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 android:name=".MyReceiver" >
            <intent-filter android:priority="20">
                <action android:name="com.lp.action.WELCOME_BROADCAST"/>
            </intent-filter>
        </receiver>
        <receiver android:name=".MyReciever2" >
            <intent-filter android:priority="0">
                <action android:name="com.lp.action.WELCOME_BROADCAST"/>
            </intent-filter>
        </receiver>
    </application>
    
</manifest>

step6效果图:分析:
该程序中包含两个BroadcastReceiver,分别为MyReciever,MyReciever2,由于MyReciever的优先级高,则如果加上abortBroadcast();将阻止消息的传播,不会传到MyReciever2,如果abortBroadcast();注释掉,则程序MyReciever2的OnReceive方法可以触发,并解析得到MyReciever存入结果中的key为first的消息。结果图:
  MyReciever中接受到的内容:
  广播接收者BroadcastReceiver
 MyReciever2:
广播接收者BroadcastReceiver
其他广播Intent如
<action android:name="android.intent.action.BATTERY_CHANGED"/>电池电量变化广播Intent
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>短信
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>电话