Android學習之Android廣播機制

來源:互聯網
上載者:User

一提起廣播,我們首先想到的是收音機,當我們想要收聽某個廣播台時只需要將收音機的頻率調至廣播台所在的頻率即可!而Android中的廣播其實和收音機非常相似,不過它沒有所謂的頻率,它是由系統廣播一個事件,然後由其他滿足某一條件的程式接收並處理這個事件!!

  要在Android中實現廣播,首先我們要在Manifest.xml檔案中配置一個<receiver/>標籤,這個標籤必須有一個android:name屬性,值為繼承自BroadcastReceiver類的接收器類!這個標籤還有一個子標籤為<intent-filter/>,這個標籤很重要,是指定接收器需要接收哪種廣播。另外,還有配置一個使用者權限:<uses-permission/>,具體的值可以參考官方API文檔。

另外一個比較重要的步驟是必須有一個類繼承自BroadcastReceiver類,並複寫onReceiver方法,在該方法中處理接收到廣播後需要處理的事情!

下面來看一個具體的例子,有助於更好的理解廣播機制是怎麼一回事。

UI部分就不說了,Activity上就加了一個按鈕,點擊後發送廣播。接收器接收到廣播後在終端輸出一句話。

首先看AndroidManifest.xml檔案:


<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.gufengxiachen.broadcast"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="8" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".BroadCast"                  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="android.intent.action.EDIT"></action></intent-filter></receiver>    </application>       <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission></manifest>

下面是Activity:


package com.gufengxiachen.broadcast;import android.app.Activity;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class BroadCast extends Activity {    /** Called when the activity is first created. */private Button sendBroadCast =null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);               sendBroadCast = (Button)findViewById(R.id.sendBroadCast);               sendBroadCast.setOnClickListener(new SendBroadCast());    }    class SendBroadCast implements OnClickListener{    @Override    public void onClick(View v) {    // TODO Auto-generated method stub        Intent intent = new Intent(Intent.ACTION_EDIT);    BroadCast.this.sendBroadcast(intent);    }    }      }

下面是接收器類:

package com.gufengxiachen.broadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;public class MyBroadCastReceiver extends BroadcastReceiver{public MyBroadCastReceiver(){}@Overridepublic void onReceive(Context context, Intent intent) {System.out.println("message receiver");}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.