android的廣播介紹,在程式中註冊廣播

來源:互聯網
上載者:User

利用代碼註冊 BroadcastReceiver 時有以下步驟:

   1)產生一個 BroadcastReceiver 對象;

   2)產生一個 IntentFilter 對象;

   3)為 IntentFilter 對象添加一個 Action ;

   4)利用 IntentFilter 和 BroadcastReceiver 綁定註冊一個 BroadcastReceiver 到系統中,當系統廣播該Action事件的時候,就出發該BroadcastReceiver。

1 首先定義一個activity

public class BroadcastDemoActivity extends Activity {    private Button regButton,unregButton;    final static String SMSReceiver = "android.provider.Telephony.SMS_RECEIVED";//接收簡訊的廣播    private MyBroadReceiver myBroadReceiver;//廣播接收者    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                initView();    }private void initView() {regButton = (Button) this.findViewById(R.id.reg);regButton.setOnClickListener(new RegListener());//這裡不能在內部類中註冊廣播,會報錯unregButton = (Button) this.findViewById(R.id.reg);unregButton.setOnClickListener(new RegListener());}private class RegListener implements View.OnClickListener{public void onClick(View v) {switch(v.getId()){case R.id.reg:myBroadReceiver = new MyBroadReceiver();//此對象用於接收廣播,IntentFilter filter = new IntentFilter();filter.addAction(SMSReceiver);//註冊一個接收簡訊的廣播//將廣播接收者註冊到系統中BroadcastDemoActivity.this.registerReceiver(myBroadReceiver, filter);break;case R.id.unreg://從系統中解除廣播接收者BroadcastDemoActivity.this.unregisterReceiver(myBroadReceiver);break;}}}}

2,定義一個接收廣播的類,這裡簡單處理,只列印一個廣播到來的資訊。

public class MyBroadReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {Log.i("MyBroadReceiver", "sms is comming");}}

3,由於訂閱了接收簡訊的廣播,所以要聲明接收簡訊的許可權

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.cjf.broadcast"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="8" />    <uses-permission android:name="android.permission.RECEIVE_SMS"/><!-- 接收簡訊許可權 -->    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:name=".BroadcastDemoActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

4,程式碼完成,點擊註冊按鈕,然後用模擬器發送一條簡訊。LogCat會列印出簡訊到來的資訊

聯繫我們

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