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

步步为营_Android开发课[7]_BroadCastReceiver学习

2015-04-01 20:03 344 查看
Focus on technology, enjoy life!—— QQ:804212028

浏览链接:/article/1513032.html

主题:BroadCastReceiver学习

BroadCastReceiver作用:

Broadcast Receiver用于接收并处理广播通知(broadcast announcements)。多数的广播是系统发起的,如地域变换、电量不足、来电来信等。程序也可以播放一个广播。程序可以有任意数量的 broadcast receivers来响应它觉得重要的通知。broadcast receiver可以通过多种方式通知用户:启动activity、使用NotificationManager、开启背景灯、振动设备、播放声音等,最典型的是在状态栏显示一个图标,这样用户就可以点它打开看通知内容。

注册广播的两种方式,它们有何不同?

答:首先写一个类要继承BroadcastReceiver,然后注册声明:

第一种:在清单文件中声明添加

<receiver android:name=".MyBroadcastReceiver">
            <intent-filter>
                <action android:name="com.example.mybroadcastreceiver.hello"/>
            </intent-filter>
        </receiver>


第二种在Activity中使用代码进行注册如:

IntentFilter filter =  new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
//MyBroadcastReceiver是自定义的类,需要继承BroadcastReceiver类
MyBroadcastReceiver receiver = new MyBroadcastReceiver();
registerReceiver(receiver.filter);


两种注册类型的区别是:

1)第一种是常驻型,也就是说当应用程序关闭后,如果有信息广播来,程序也会被系统调用自动运行。

2)第二种不是常驻型广播,也就是说广播跟随程序的生命周期。

实例一(第一种注册方式)

AndroidMainfest.xml源代码:

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

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

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mybroadcastreceiver.MainActivity"
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=".MyBroadcastReceiver"> <intent-filter> <action android:name="com.example.mybroadcastreceiver.hello"/> </intent-filter> </receiver>
</application>

</manifest>


MainActivity.java源代码:

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 MainActivity extends Activity {

    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.button01);
        btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent().setAction("com.example.mybroadcastreceiver.hello")
                        .putExtra("flag", "hello,小嘟嘟");
                sendBroadcast(intent);
            }
        });     
    }
}


MyBroadcastReceiver.java源代码:

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

public class MyBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // 手机开机的广播
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Log.i("----------","BOOT_COMPLETED !!!!!!!!!!");
        }
        //之前定义好的广播
        if(intent.getAction().equals("com.example.mybroadcastreceiver.hello")){
            Log.v("----------", intent.getStringExtra("flag"));
        }
        //顺便来首歌曲《浩瀚》,带点气氛。
        MediaPlayer.create(context, R.raw.haohan).start();
    }

}


实例二(第二种注册方式)

AndroidMainfest.xml源代码:

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.mybroadcastreceiver.MainActivity"
            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>


MainActivity.java源代码:

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.widget.Button;

public class MainActivity extends Activity {

    MyBroadcastReceiver mbr;
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b1 = (Button) findViewById(R.id.button01);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("click", "OnClick");
                mbr = new MyBroadcastReceiver();
                IntentFilter filter = new IntentFilter();
                filter.addAction("android.provider.Telephony.SMS_RECEIVED");
                registerReceiver(mbr, filter);

                sendBroadcast(new Intent("android.provider.Telephony.SMS_RECEIVED"));
            }
        });
    }
}


MyBroadcastReceiver.java源代码:

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

public class MyBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
         if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
                Log.v("------------",
                        "接收到了广播!!!!!!!!!!!!!!!");
            }       
    }
}


Focus on technology, enjoy life!—— QQ:804212028

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