Android從無知到有知——NO.7,androidno.7

來源:互聯網
上載者:User

Android從無知到有知——NO.7,androidno.7

 

          前面做的ip撥號器在監聽撥出電話時用的是系統提供的廣播事件,而有些時候我們需要自己設定廣播事件來滿足特定的需要。Ok,今天整一下自訂廣播事件,我們用一個狀態監測模組向一個3G模組發送警示資訊來實現這一想法。

         先定義一個3g模組用來接收特定的廣播:

<TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:text="俺是一個3g模組~~~~" />

         

        然後設定它的自訂廣播事件:

<receiver android:name=".MyBro">            <intent-filter>                <action android:name="com.heng.lh"/>            </intent-filter></receiver>

          這裡的com.heng.lh是我們自己定義的廣播事件,用來接收特定頻道的訊號。

             然後讓我們“吐絲”一下,如果有自訂的廣播發過來便給使用者一個提示。

public void onReceive(Context context, Intent intent) {Toast.makeText(context, "檢測到一個廣播事件",1).show();}


         接收廣播的模組做好了,然後就要設定發送廣播的模組了,主介面需要一個按鈕來向3G模組發送警示資訊:

<Button        android:onClick="click"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:text="向3G模組發送資訊" />

         我們來寫一下這個點擊事件,new出來一個意圖,然後設定它的動作,也就是我們前面自訂的“com.heng.lh”,最後用sendBroadcast把資訊發送出去。

public void click(View view){Intent intent=new Intent();intent.setAction("com.heng.lh");//把警示資訊發送給3G模組sendBroadcast(intent);}

         這樣3G模組便會收到一個廣播資訊,一個簡單的自訂廣播事件也就建立好了。


作業答案:我們要從無知到有知,

B
 
從無知到有知的意思是從不懂到懂得的意思,也可以說是從不成熟到成熟的一個意思不

從無知到不無知就是你覺得一些話是廢話的時候還是用肯定的態度耐心地去接受。
 

聯繫我們

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