歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:BroadcastReceiver簡介和注冊方式

Android開發教程:BroadcastReceiver簡介和注冊方式

日期:2017/3/1 10:36:43   编辑:Linux編程
一.BroadcastReceiver簡介

BraodcastReceiver(廣播接收器)是為了實現系統廣播而提供的一種組件,它和事件處理機制類似,但是事件處理機制是程序組件級別的,而廣播事件處理機制是系統級別的。比如,我們可以發出一種廣播來測試手機電量的變化,這時候就可以定義一個BraodcastReceiver來接受廣播,當手機電量較低時提示用戶。我們既可以用Intent來啟動一個組件,也可以用sendBroadcast()方法發起一個系統級別的事件廣播來傳遞消息。我們同樣可以在自己的應用程序中實現BroadcastReceiver來監聽和響應廣播的Intent。

在程序中使用BraodcastReceiver是比較簡單的。首先要定義一個類繼承BraodcastReceiver,並且覆蓋onReceiver()方法來響應事件。然後注冊在程序中BraodcastReceiver。最後構建Intent對象調用sendBroadcast()方法將廣播發出。

二.BroadcastReceiver的注冊方式

1.靜態注冊方式

靜態注冊方式是在AndroidManifest.xml的application裡面定義receiver並設置要接收的action。靜態注冊方式的特點:不管改應用程序是否處於活動狀態,都會進行監聽,比如某個程序時監聽 內存 的使用情況的,當在手機上安裝好後,不管改應用程序是處於什麼狀態,都會執行改監聽方法中的內容。

下面是具體的例子:

MainActivity.java

  1. package com.android.broadcast;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. public class MainActivity extends Activity{
  9. //定義action常量
  10. protected static final String ACTION = "com.android.broadcast.RECEIVER_ACTION";
  11. //定義Button對象
  12. private Button btnBroadcast;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. btnBroadcast=(Button)findViewById(R.id.btnBroadcast);
  18. //為按鈕設置單擊監聽器
  19. btnBroadcast.setOnClickListener(new OnClickListener(){
  20. @Override
  21. public void onClick(View v){
  22. //實例化Intent
  23. Intent intent=new Intent();
  24. //設置Intent的action屬性
  25. intent.setAction(ACTION);
  26. //發出廣播
  27. sendBroadcast(intent);
  28. }
  29. });
  30. }
  31. }

在“com.android.broadcast”包中定義一個MyReceiver類,繼承於BroadcastReceiver,覆蓋onReceive()方法。

MyReceiver.java

  1. package com.android.broadcast;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. public class MyReceiver extends BroadcastReceiver{
  7. //定義日志標簽
  8. private static final String TAG = "Test";
  9. @Override
  10. public void onReceive(Context context, Intent intent){
  11. //輸出日志信息
  12. Log.i(TAG, "MyReceiver onReceive--->");
  13. }
  14. }

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <Button
  8. android:id="@+id/btnBroadcast"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="發送Broadcast"
  12. />
  13. </LinearLayout>

在AndroidManifest.xml配置文件中16~20行聲明receiver

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.android.broadcast"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdk android:minSdkVersion="10" />
  7. <application android:icon="@drawable/icon" android:label="@string/app_name">
  8. <activity android:name=".MainActivity"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <action android:name="android.intent.action.MAIN" />
  12. <category android:name="android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </activity>
  15. <receiver android:name="MyReceiver">
  16. <intent-filter>
  17. <action android:name="com.android.broadcast.RECEIVER_ACTION"/>
  18. </intent-filter>
  19. </receiver>
  20. </application>
  21. </manifest>

效果圖:

650) this.width=650;" height=132>

當我們點擊650) this.width=650;" height=120>按鈕的時候,程序會調用onReceive()方法,LogCat輸出信息如下:

650) this.width=650;" height=120>

Copyright © Linux教程網 All Rights Reserved