Android四大組件之—— BroadcastReceiver的使用,broadcastreceiver

來源:互聯網
上載者:User

Android四大組件之—— BroadcastReceiver的使用,broadcastreceiver

 

BroadcastReceiver又名廣播接收者。既然它用於接收廣播,那一定就有人負責發送。

 

Android系統中的廣播:

在現實生活中,我們都知道廣播是什麼,用來做什麼。例如公園裡的廣播,主要通知遊客什麼事情發生了,應該做什麼,

不應該做什麼。Android系統中的廣播跟現實生活中的基本一樣,主要用於訊息的傳遞。

廣播接收者可在java代碼中動態註冊,也可以在AndroidManifest檔案中註冊

 

有序廣播和無序廣播

先來說一下無序廣播。還是用公園裡的廣播作為例子,只要廣播一發送出去,所有人基本上都同時聽到。

它是非同步傳輸的,這就是無序廣播。它的特點是傳輸速度快。但有序廣播不能被攔截和被終止。

無序廣播使用sendBroadcast方法發送。

有序廣播按照一定的順序發送出去,許可權高的接收者先接收到廣播資訊,該接收者可修改廣播中的資訊,

然後再將廣播向下級傳播;也可以終止廣播的傳播。

 

 

BroadcastReceiver類的建立

通過簡單的繼承BroadcastReceive類並實現onReceive方法就可以建立自己的廣播接收者類

private class NormalBroadcastReceiver extends BroadcastReceiver{      @Override      public void onReceive(Context context, Intent intent)      {           String data = intent.getStringExtra("data");             Toast.makeText(getBaseContext(), data, Toast.LENGTH_SHORT).show();      }    }

 

完整代碼:

在下面的程式碼片段中,我們通過onResume方法註冊廣播接收者,在onPause方法中取消廣播接收者的註冊

當然了,也可以在AndroidManifest檔案中註冊,這樣的廣播接收者一旦註冊就不能取消。

package com.whathecode.broadcastreceiverdemo;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity{        //執行個體化廣播接收者    NormalBroadcastReceiver nbr = new NormalBroadcastReceiver();        @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }        @Override    protected void onResume()    {        super.onResume();        //註冊廣播接收者        registerReceiver(nbr, new IntentFilter("com.whathecode.broadcastreceiverdemo"));    }        @Override    protected void onPause()    {        super.onPause();        //取消註冊廣播接收者        unregisterReceiver(nbr);    }        public void sendOrder(View view)    {        Intent orderBroadcast = new Intent();        orderBroadcast.setAction("com.whathecode.broadcastreceiverdemo");        orderBroadcast.putExtra("data", "我是有序廣播");        //發送有序廣播        sendOrderedBroadcast(orderBroadcast, null);    }        public void sendDisorder(View view)    {        Intent intent = new Intent();        intent.setAction("com.whathecode.broadcastreceiverdemo");        intent.putExtra("data", "我是無序廣播");        //發送無序廣播        sendBroadcast(intent);    }            //繼承BroadcastReceiver類,實現onReceive方法    private class NormalBroadcastReceiver extends BroadcastReceiver    {        /**         * 當廣播被接收的時候這個方法被調用         */        @Override        public void onReceive(Context context, Intent intent)        {            //擷取廣播中的資訊並用Toast列印出來            String data = intent.getStringExtra("data");                        Toast.makeText(getBaseContext(), data, Toast.LENGTH_SHORT).show();        }            }}

 

運行效果:

聯繫我們

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