Android開發之BroadcastReceiver用法執行個體分析

來源:互聯網
上載者:User

Android開發之BroadcastReceiver用法執行個體分析

   本文執行個體講述了Android開發中BroadcastReceiver用法。分享給大家供大家參考。具體分析如下:

  在Android系統中,廣播(Broadcast)是在組件之間傳播資料(Intent)的一種機制。

  Braodcast Receiver顧名思義就是廣播接收器,它和事件處理機制類似,但是事件處理機制是程式組件層級的(比如:按鈕的單擊事件),而廣播事件處理機制是系統層級的。我們可以用Intent來啟動一個組件,也可以用sendBroadcast()方法發起一個系統層級的事件廣播來傳遞訊息。我們同樣可以在自己的應用程式中實現Broadcast Receiver來監聽和響應廣播的Intent。

  事件的廣播通過建立Intent對象並調用sendBroadcast()方法將廣播發出。事件的接受是通過定義一個繼承BroadcastReceiver的類來實現的,繼承該類後覆蓋其onReceive()方法,在該方法中響應事件。

  下面是android系統中定義了很多標準的Broadcast Action來響應系統的廣播事件。

  ①ACTION_TIME_CHANGED(時間改變時觸發)

  ②ACTION_BOOT_COMPLETED(系統啟動完成後觸發)--比如有些程式開機後啟動就是用這種方式來實現的

  ③ACTION_PACKAGE_ADDED(添加包時觸發)

  ④ACTION_BATTERY_CHANGED(電量低時觸發)

  下面看一個例子:

  我們在一個按鈕上綁定一個事件,事件通過發送一個廣播來觸發logcat打出一個log。

  先看manifest檔案。

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

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

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

package="com.example.test"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="16" />

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="com.example.broadcast.BroadcastReceiverActivity"

android:label="@string/app_name_bc" >

<intent-filter>

<action android:name="android.intent.action.MAIN" >

</action>

<category android:name="android.intent.category.LAUNCHER" >

</category>

</intent-filter>

</activity>

<receiver android:name="com.example.broadcast.HelloBroadReciever" >

<intent-filter>

<action android:name="comz.test.printlog" >

</action>

</intent-filter>

</receiver>

</application>

</manifest>

  上面聲明了一個receiver。接收名字是comz.test.printlog的訊息。

  看activity:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

package com.example.broadcast;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import com.example.test.R;

public class BroadcastReceiverActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button b1 = (Button) findViewById(R.id.broadcastBtn);

b1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Log.e("mason", "here");

// 定義一個intent

Intent intent = new Intent().setAction("comz.test.printlog")

.putExtra("info", "here is your info.");

// 廣播出去

sendBroadcast(intent);

}

});

}

}

  在這段代碼中,定義一個intent並發送廣播出去。

  看BroadReceiver的代碼:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

package com.example.broadcast;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.util.Log;

public class HelloBroadReciever extends BroadcastReceiver {

// 如果接收的事件發生

@Override

public void onReceive(Context context, Intent intent) {

Log.e("mason", "on receive");

if (intent.getAction().equals("comz.test.printlog")) {

Log.e("mason", intent.getStringExtra("info"));

}

}

}

  這是BroadcastReceiver的代碼。

  在接收到訊息之後,如果訊息是comz.test.printlog,則列印訊息。

  希望本文所述對大家的Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.