Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android技術基礎 >> 第91章、廣播之二自定義電台與聽眾(從零開始學Android)

第91章、廣播之二自定義電台與聽眾(從零開始學Android)

編輯:Android技術基礎

廣播,這個詞我們不會陌生,不管你聽不聽,我們都懂!

收聽收音機就是一種廣播,在收音機中有多個廣播電台,每個廣播電台播放的內容都不相同。廣播電台主持人(發送方)並不在意我們(接收方)聽到廣播內容之後會如何處理。譬如我們聽到路況信息的廣播,電台廣播(發送方)告訴我們目前交通狀況如何,但它並不關心我們接收到廣播時做如何做出處理,這不是廣播應該關心的問題!
我們(接收方)可能很關心,開車選擇另一條線路;也可能我們“聽而不見”,可能這個線路擁堵與我們無關!
Android 中的廣播與之大同小異。

Android 的廣播機制

在Android 裡面有各種各樣的廣播,比如電池的使用狀態,電話的接收和短信的接收都會產生一個廣播,應用程序開發者也可以監聽這些廣播並做出程序邏輯的處理。下面我畫一張粗略的圖來幫助大家理解廣播的運行機制。

自創建廣播步驟:

(1)定義BroadcastReceiver廣播接收類

(2)發送廣播

(3)注冊廣播(接收器與過濾器) 

一、設計界面

1、布局文件

打開res/layout/activity_main.xml文件。
輸入以下代碼:

[html] view plain copy  
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2.   
  3. <LinearLayout   
  4.     xmlns:android="http://schemas.android.com/apk/res/android"   
  5.     android:orientation="vertical"   
  6.     android:layout_width="fill_parent"   
  7.     android:layout_height="fill_parent">  
  8.   
  9.     <Button  
  10.         android:id="@+id/send"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="發送廣播" />  
  14.   
  15. </LinearLayout>  


二、程序文件

1、BroadcastReceiverUtil.java
打開“src/com.genwoxue.broadcastdiy/BroadcastReceiverUtil.java”文件。
然後輸入以下代碼:

[java] view plain copy  
  1. package com.genwoxue.broadcastdiy;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.widget.Toast;  
  7.   
  8. //聲明廣播接收類,重寫onReceive()方法用於獲取手機當前日期和時間  
  9. public class BroadcastReceiverUtil extends BroadcastReceiver{  
  10.       
  11.     @Override  
  12.     public void onReceive(Context context,Intent intent){  
  13.         if("com.genwoxue.action.CURRTIME".equals(intent.getAction())){  
  14.             String info=intent.getStringExtra("currdatetime");  
  15.             Toast.makeText(context, info, Toast.LENGTH_LONG).show();  
  16.         }  
  17.           
  18.     }  
  19. }  

2、MainActivity.java
打開“src/com.genwoxue.broadcastdiy/MainActivity.java”文件。
然後輸入以下代碼:

[java] view plain copy  
  1. package com.genwoxue.broadcastdiy;  
  2.   
  3. import java.text.DateFormat;  
  4. import java.util.Date;  
  5.   
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.app.Activity;  
  11. import android.content.Intent;  
  12. import android.content.IntentFilter;  
  13.   
  14. public class MainActivity extends Activity {  
  15.   
  16.     private Button btnSend=null;  
  17.     private BroadcastReceiverUtil util=null;  
  18.       
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.           
  24.         btnSend=(Button)super.findViewById(R.id.send);  
  25.         btnSend.setOnClickListener(new OnClickListener(){  
  26.             @Override  
  27.             public void onClick(View v){  
  28.                   
  29.                 //顯示日期,周,時間(精確到秒)  
  30.                 Date now = new Date();  
  31.                 DateFormat currDateTime = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);   
  32.                   
  33.                 //發送廣播,Action為“com.genwoxue.action.CURRTIME”  
  34.                 Intent intent=new Intent("com.genwoxue.action.CURRTIME");  
  35.                 intent.putExtra("currdatetime", currDateTime.format(now));  
  36.                 MainActivity.this.sendBroadcast(intent);  
  37.   
  38.                 //實例化廣播過濾器(只過濾當前時間,其Action為"com.genwoxue.action.CURRTIME")  
  39.                 IntentFilter filter=new IntentFilter("com.genwoxue.action.CURRTIME");  
  40.                 //實例化廣播接收器(接收方)  
  41.                 util=new BroadcastReceiverUtil();  
  42.                 //注冊BroadcastReceiver:參數為接收器與過濾器  
  43.                 MainActivity.this.registerReceiver(util, filter);  
  44.                   
  45.             }  
  46.         });  
  47.     }  
  48.       
  49.     @Override  
  50.     protected void onStop(){  
  51.         super.unregisterReceiver(util);  
  52.         super.onStop();  
  53.     }  
  54. }  

本案例中,廣播發送方其Action為“com.genwoxue.action.CURRTIME”,發送廣播內容為當前系統日期和時間;廣播接收方僅“聽”(處理)與Action為“com.genwoxue.action.CURRTIME”的有關的信息,本例自然是收聽廣播的時間並顯示出來。

三、配置文件

AndroidManifest.xml采用默認即可,無需另行配置。

四、運行結果

\

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved