步步爲營_Android開發課[7]_BroadCastReceiver學習

Focus on technology, enjoy life!—— QQ:804212028
瀏覽鏈接:http://blog.csdn.net/y18334702058/article/details/44624305


  • 主題: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
瀏覽鏈接:http://blog.csdn.net/y18334702058/article/details/44624305

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章