Android Broadcaset 简介

在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制。而BroadcastReceiver是对发送出来的Broadcast进行过滤接收并响应的一类组件。可以使用BroadcastReceiver来让应用对一个外部的事件作出响应。这是非常有意思的,例如,当电话呼入这个外部事件到来的时候,可以利用BroadcastReceiver进行处理。又如,当下载一个程序成功完成的时候,仍然可以利用BroadcastReceiver进行处理。 BroadcastReceiver不能生成UI,因此用户看不到相应的界面。
BroadcastReceiver通过NotificatiooManager来通知用户这些事情发生了。BroadcastReceiver既可以在AndroidManifest.xml中注册,也可以在运行时的代码中使用Context.registerReceiver()进行注册。只要是注册了,当事件来临的时候,即使程序没有启动,系统也会在需要的时候启动程序。各种应用还可以通过使用Context,sendBroadcast()将它们自己的intent broadcasts广播给其他应用程序。
注册BroadcastReceiver有两种方式。
(1)在AndroidManifest.xml进行注册。这种方法有一个特点,即使你的应用程序已经关闭了,但这个BroadcastReceiver依然会接受广播出来的对象,也就是说无论这个应用程序是开还是关,都属于活动状态,都可以接收到广播的事件。
(2)在代码中注册广播。
第一种俗称静态注册,第二种俗称动态注册,这两种注册BroadcastReceiver的区别是:动态注册较静态注册灵活。实验证明,当静态注册一个BroadcastReceiver时,不论应用程序启动与否,都可以接受对应的广播。动态注册的时候,如果不执行umegisterReceiver()方法取消注册,跟静态是一样的。但是如果执行该方法,当执行过以后,就不能接受广播了。

MyBroadCastActivity.java

package com.supermario.mybroadcast;

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;

import java.util.Date;



public class MyBroadCastActivity extends Activity {

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

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //发送广播按钮

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

        //设置发送广播对应的intent

        final Intent intent=new Intent("com.guo.receiver.myreceiver");

        intent.putExtra("name", "AndyGe");

        intent.putExtra("age", 36);

        btn.setOnClickListener(new OnClickListener(){

            @Override

            public void onClick(View arg0) {

                // TODO Auto-generated method stub

                //发送广播

                sendBroadcast(intent);

            }           

        });

    }

}

MyReceiver.java

package com.supermario.mybroadcast;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

    @Override

    public void onReceive(Context arg0, Intent arg1) {

        // TODO Auto-generated method stub

        StringBuilder sb = new StringBuilder();

        sb.append("Name:"+arg1.getStringExtra("name"));

        sb.append("\nAge:"+arg1.getIntExtra("age", 0));

        Toast.makeText(arg0, sb.toString(), Toast.LENGTH_SHORT).show();

    }

}

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/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="发送广播" />

</LinearLayout>

AndroidManifest.xml

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

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

    package="com.supermario.mybroadcast"

    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=".MyBroadCastActivity"

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

        <receiver android:name=".MyReceiver">

            <intent-filter>

                <action android:name="com.guo.receiver.myreceiver" />

            </intent-filter>

        </receiver>

    </application>

</manifest>

 

PIC_20131119_152248_110

你可能感兴趣的:(android)