Android提高第六篇之BroadcastReceiver

來源:互聯網
上載者:User

前面分別討論了html" target=_blank>Activity和Service,這次就輪到BroastcastReceiver,Broastcast是應用程式間通訊的手段。BroastcastReceiver也是跟Intent緊密相連的,動態/靜態註冊了BroastcastReceiver之後,使用sendBroadcast把Intent發送之後,系統會自動把合格BroastcastReceiver啟動,跟嵌入式系統的中斷類似。

        本文主要示範了如何靜態/動態註冊BroastcastReceiver,向系統索取電量資訊,以及枚舉資訊的欄位。本文運行如下:

                       

 


是發送Intent至內部動態註冊的BroadcastReceiver,接收到之後顯示訊息名稱。動態註冊BroadcastReceiver用到registerReceiver()。

 

 

是發送Intent至內部靜態註冊的BroadcastReceiver,接收到之後顯示訊息名稱。靜態註冊比動態註冊麻煩點,先建立一個類繼承BroadcastReceiver,然後到AndroidManifest.xml 添加

view plaincopy to clipboardprint?
<receiver android:name="clsReceiver2"> 
    <intent-filter> 
        <action 
            android:name="com.testBroadcastReceiver.Internal_2"/> 
    </intent-filter> 
</receiver> 
  <receiver android:name="clsReceiver2">
   <intent-filter>
    <action
     android:name="com.testBroadcastReceiver.Internal_2"/>
   </intent-filter>
  </receiver>

第一個name是類名,第二個是action的名稱。

 

是枚舉Intent訊息的欄位,這個功能比較適合懶人,把收到的Intent訊息的欄位全部分解了,再看看哪個需要的,懶得記住。實現這部分的代碼如下:


view plaincopy to clipboardprint?
//當未知Intent包含的內容,則需要通過以下方法來列舉  
                Bundle b=intent.getExtras();  
                Object[] lstName=b.keySet().toArray();  
 
                for(int i=0;i<lstName.length;i++)  
                {  
                    String keyName=lstName[i].toString();  
                    Log.e(keyName,String.valueOf(b.get(keyName)));  
                } 
//當未知Intent包含的內容,則需要通過以下方法來列舉
    Bundle b=intent.getExtras();
    Object[] lstName=b.keySet().toArray();

    for(int i=0;i<lstName.length;i++)
    {
     String keyName=lstName[i].toString();
     Log.e(keyName,String.valueOf(b.get(keyName)));
    }

main.xml的代碼如下:

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
 
    <Button android:id="@+id/Button01" android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:text="發送至內部動態註冊的BroadcastReceiver"></Button> 
    <Button android:id="@+id/Button02" android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:text="發送至內部靜態註冊BroadcastReceiver"></Button> 
    <Button android:id="@+id/Button03" android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:text="發送至系統BroadcastReceiver"></Button> 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">

 <Button android:id="@+id/Button01" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:text="發送至內部動態註冊的BroadcastReceiver"></Button>
 <Button android:id="@+id/Button02" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:text="發送至內部靜態註冊BroadcastReceiver"></Button>
 <Button android:id="@+id/Button03" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:text="發送至系統BroadcastReceiver"></Button>
</LinearLayout>
 

testBroadcastReceiver.java的代碼如下:

view plaincopy to clipboardprint?
package com.testBroadcastReceiver;  
 
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.util.Log;  
import android.view.View;  
import android.widget.Button;  
import android.widget.Toast;  
 
public class testBroadcastReceiver extends Activity {  
    Button btnInternal1,btnInternal2,btnSystem;  
    static final String INTENAL_ACTION_1 = "com.testBroadcastReceiver.Internal_1";  
    static final String INTENAL_ACTION_2 = "com.testBroadcastReceiver.Internal_2";  
    static final String INTENAL_ACTION_3 = "com.testBroadcastReceiver.Internal_3";  
    @Override 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        btnIn

聯繫我們

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