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
從無知到有知的意思是從不懂到懂得的意思,也可以說是從不成熟到成熟的一個意思不
從無知到不無知就是你覺得一些話是廢話的時候還是用肯定的態度耐心地去接受。