初學Android,BroadcastReceiver之發送接收廣播

來源:互聯網
上載者:User

標籤:

BroadcastReceiver用於監聽系統全域廣播訊息,由於BroadcastReceiver是一種全域的監聽器,因此它可以非常方便地實現系統中不同組件之間通訊

啟動它需要兩步

1.建立需要啟動的BroadcastReceiver的Intent

2.調用Context的SendBroadcast或sendOrederedBroadcast方法來啟動指定的BroadcastReceiver

這其中關鍵是建立Intent時,要setAction("xxx"),而BroadcastReceiver就要建立它的filter,或在代碼中指定,或在xml中指定

下面以一個簡單例子說明,例子是一個向廣播發送訊息的程式

發送端主介面代碼

[java] view plaincopyprint? 
  1. package WangLi.Service.Broadcast;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.   
  10. public class Broadcast extends Activity {  
  11.     Button send;  
  12.   
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         // 擷取介面中的按鈕  
  18.         send = (Button) findViewById(R.id.send);  
  19.         send.setOnClickListener(new OnClickListener() {  
  20.             @Override  
  21.             public void onClick(View arg0) {  
  22.                 // 建立Intent對象  
  23.                 Intent intent = new Intent();  
  24.                 // 設定Intent的Action屬性  
  25.                 intent.setAction("WangLi.Test");  
  26.                 intent.putExtra("msg", "簡單的訊息");  
  27.                 //發送廣播  
  28.                 sendBroadcast(intent);  
  29.             }  
  30.         });  
  31.     }  
  32. }  

接著建立一個BroadcastReceiver監聽器

[java] view plaincopyprint? 
  1. package WangLi.Service.Broadcast;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.widget.Toast;  
  7.   
  8. public class MyReceiver extends BroadcastReceiver {  
  9.   
  10.     @Override  
  11.     public void onReceive(Context context, Intent intent) {  
  12.         Toast.makeText(  
  13.                 context,  
  14.                 "接收到的Intent的Action為:" + intent.getAction() + "\n訊息內容是:"  
  15.                         + intent.getStringExtra("msg"), 5000).show();  
  16.     }  
  17.   

向AndroidManifest.xml中配置該receiver,主要是intent filter

 

[html] view plaincopyprint? 
  1. <receiver android:name="MyReceiver" >  
  2.     <intent-filter>  
  3.         <action android:name="WangLi.Test" />  
  4.     </intent-filter>  
  5. </receiver>  

要注意的是BroadcastReceiver的到期時間為10秒,如果是耗時的操作應放在service中去完成,BroadcastReceiver只適合短時間的計算任務,因為它的生命週期非常短

初學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.