[android,20]廣播接收者

來源:互聯網
上載者:User

廣播接收者(BroadcastReceiver)一、什麼是廣播:

當一個事件產生的時候,就會發出一個廣播,所有註冊了這個廣播的接受者,都可以接收到這個廣播。

 

二、定義廣播接收者步驟:(簡訊的監聽器:接收簡訊的到來時發出的廣播,並擷取簡訊的內容)

1、自訂一個類,繼承BroadcastReceiver類。覆寫onReceive方法

public class SmsReceiver extends BroadcastReceiver {

publicvoid onReceive(Context arg0, Intent data) {

System.out.println("簡訊到來了");

//pdus為簡訊的規範,擷取簡訊的數組。

Object[] pdus = (Object[])data.getExtras().get("pdus");

for(Object pdu : pdus){

//擷取每一條簡訊對象

SmsMessage smsmessage = SmsMessage.createFromPdu((byte[]) pdu);

//簡訊的內容:

Stringcontent = smsmessage.getMessageBody();

System.out.println(content);

if(content.contains("fapiao")){

abortBroadcast();

}

//擷取簡訊的寄件者

Stringsender = smsmessage.getOriginatingAddress();

System.out.println(sender);

 

if("5558".equals(sender)){

abortBroadcast();

//擷取簡訊的管理者

SmsManager manager =SmsManager.getDefault();

//向寄件者發簡訊

manager.sendTextMessage(sender, null,"ni qu siba ", null, null);

}

}

 

// 終止掉這條廣播事件

}

 

2、在AndroidManifest.xml資訊清單檔中使用receiver 元素配置廣播接收者:

 

package="cn.itcast.listener"

android:versionCode="1"

android:versionName="1.0" >

 

 

 

<>

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

//配置廣播接收者

 

//廣播接收者的意圖過濾器。

 

//接收簡訊的動作

android:name="android.provider.Telephony.SMS_RECEIVED"/>

 

 

 

 

 

 

3、注意:想要廣播接收者接收不同的事件發出的官博,就在資訊清單檔中廣播接收者的意圖過濾器中配置相應的事件。

//配置廣播接收者

 

//廣播接收者的意圖過濾器。

//指定廣播優先順序

//接收簡訊的動作

android:name="android.provider.Telephony.SMS_RECEIVED"/>

 

 

 

4、系統的廣播事件分為兩種類型:

有序廣播:按順序下達的廣播,可以指定廣播接收者的優先順序。

優先順序從-1000到1000,1000代表優先順序最大。當廣播到來時,按廣播接收者的優先順序高低傳播。如果在高優先順序的廣播接收者中調用abortBroadcast()終止掉這條廣播事件。則該廣播就不會向優先順序低的廣播接收者傳播。

例:攔截垃圾簡訊。

無序廣播:廣播接收者,同時接收這個廣播。

 

三、案例:為撥打的號碼增加IP號:

1、定義類:

public class OutGoingCallReceiver extendsBroadcastReceiver {

 

@Override

publicvoid onReceive(Context context, Intent arg1) {

//IP號

String ipnumber = "17951";

String number = getResultData(); //擷取撥打的號碼

System.out.println("當前的電話號碼為"+number);

//把ip號碼添加到 要撥打的號碼的前面

String newnumber = ipnumber+number;

setResultData(newnumber);//設定撥打的號碼

 

}

 

}

2、在資訊清單檔中註冊廣播接收者:指定接收者的接收的事件和優先順序:

<>

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

 

android:name="android.intent.action.NEW_OUTGOING_CALL"/>

-->

 

 

3、在資訊清單檔中配置撥打到電話的許可權:

android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

 

 

四、在代碼中動態註冊廣播接收者:

在activity類中

//建立廣播接收者

OutGoingCallReceiver receiver = newOutGoingCallReceiver();

//建立意圖過濾器

IntentFilter filter = new IntentFilter();

filter.setPriority(1000);//設定優先權

//設定意圖

filter.addAction("android.intent.action.NEW_OUTGOING_CALL");

//註冊廣播接收者

registerReceiver(receiver, filter);

 

五、廣播接收者的生命週期:

如果廣播接收者響應超過10秒,會導致主線程無響應。報出anr異常。

 

聯繫我們

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