Android -- BroadcastReceiver的使用

來源:互聯網
上載者:User

標籤:android

BroadcastReceiver也就是“廣播接收者”的意思,顧名思義,它就是用來接收來自系統和應用中的廣播。
在Android系統中,廣播體現在方方面面,例如當開機完成後系統會產生一條廣播,接收到這條廣播就能實現開機啟動服務的功能;當網路狀態改變時系統會產生一條廣播,接收到這條廣播就能及時地做出提示和儲存資料等操作;當電池電量改變時,系統會產生一條廣播,接收到這條廣播就能在電量低時告知使用者及時儲存進度,等等。
Android中的廣播機制設計的非常出色,很多事情原本需要開發人員親自操作的,現在只需等待廣播告知自己就可以了,大大減少了開發的工作量和開發週期。而作為應用開發人員,就需要數練掌握Android系統提供的一個開發利器,那就是BroadcastReceiver。

BroadcastReceiver簡單使用

第一步,
先看看我們要完成的效果。
建立工程,建立一個activity並且增加一個按鈕,發送廣播到我們建立的廣播類中。

第二步,
我們建立的MyReceiver代碼如下,

package com.example.learnbroadcastreceiver;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;public class MyReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        System.out.println("接收到了訊息");    }}

第三步,
為activity頁面的按鈕增加事件,按鈕發送訊息給MyReceiver,

    @Override    public void onClick(View v) {        switch(v.getId()){        case R.id.button1:            sendBroadcast(new Intent(this, MyReceiver.class));            break;        }    }

第四步,
我們需要為Myreceiver註冊,在\AndroidManifest.xml中
假如到application標籤下,

        <receiver             android:name=".MyReceiver"            android:enabled="true"            android:exported="true">        </receiver>

第五步,
運行代碼,receiver成功接收到訊息。

BroadcastReceiver的註冊和登出

此時,如果我們去掉AndroidManifest.xml,BroadcastReceiver的配置,那通訊將會不好使。(上面第四步)

更多內容參考:http://blog.csdn.net/liuhe688/article/details/6955668
下面我們來結束動態綁定和解除的用法

第一步,
在頁面上我們再增加2個按鈕【綁定】、【解除綁定】

第二步,
為按鈕增加時間方法,代碼如下,

package com.example.learnbroadcastreceiver;import android.app.Activity;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;public class MainActivity extends Activity implements OnClickListener {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findViewById(R.id.button1).setOnClickListener(this);        findViewById(R.id.button2).setOnClickListener(this);        findViewById(R.id.button3).setOnClickListener(this);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public void onClick(View v) {        switch(v.getId()){        case R.id.button1://          sendBroadcast(new Intent(this, MyReceiver.class));            Intent intent = new Intent("com.example.learnbroadcastreceiver.intent.action.MyReceiver");            sendBroadcast(intent);            break;        case R.id.button2:            if(myReceiver == null){                myReceiver = new MyReceiver();                registerReceiver(myReceiver, new IntentFilter("com.example.learnbroadcastreceiver.intent.action.MyReceiver"));            }            break;        case R.id.button3:            if(myReceiver != null){                unregisterReceiver(myReceiver);                myReceiver = null;            }            break;        }    }    private MyReceiver myReceiver = null;}

核心代碼 32-49行,分別是 發送、綁定、解除綁定,3個操作的用法。

此時一個綁定的和解除綁定的操作完成了。

Android -- BroadcastReceiver的使用

聯繫我們

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