Broadcasts (廣播) 靜宜大學資管系 楊子青
1. 廣播? http://brianchen85.blogspot.com/2015/01/android-broadcastreceiver.html 1.整個手機是一個校園 2.學校有事情找學生,會運用廣播 3.要能聽到廣播,教室需安裝喇叭才聽得到 4.廣播可能不是呼叫全校學生,例如:資管二A王小明 5.王小明聽到廣播,才會到指定的地方
Android的廣播事件? http://www.codedata.com.tw/mobile/android-tutorial-the-5th-class-1-broadcastreceiver-alarmmanager 在系統或其他應用程式發生一些事件時,通知需要的應用程式執行一些指定的工作。 例如裝置在接到來電的時候,系統會發出一個來電的廣播事件 常見的系統廣播事件如:系統開機程序完成、裝置電量過低、時區改變、插入耳機、螢幕開啟或關閉、應用程式安裝完成或移除等
Android broadcast Intent list https://stackoverflow.com/questions/4031740/where-is-the-all-android-broadcast-intent-list 本教學使用實例: android.media.VOLUME_CHANGED_ACTION (音量改變) android.net.wifi.WIFI_STATE_CHANGED (Wifi狀態改變)
2. 建立一專案BroadCast (接收系統廣播) Empty Activity 新增一個Java Class,當作「廣播接收器」 例如命名為TestBroadcastReceiver,繼承BroadcastReceiver
TestBroadcastReceiver需實作onReceive方法(接收到廣播訊息後之處理)
TestBroadcastReceiver.java的onReceive方法之程式碼 public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")){ int newVolume = intent.getIntExtra("android.media.EXTRA_VOLUME_STREAM_VALUE", 0); int oldVolume = intent.getIntExtra("android.media.EXTRA_PREV_VOLUME_STREAM_VALUE", 0); if (newVolume != oldVolume) { Toast.makeText(context, " 音量大小調整為 : " + newVolume, Toast.LENGTH_SHORT).show(); } else if(intent.getAction().equals("android.net.wifi.WIFI_STATE_CHANGED")) { Toast.makeText(context, "Wifi狀態改變", Toast.LENGTH_SHORT).show();
MainActivity.java的程式碼 (註冊廣播訊息) protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IntentFilter intentFilter = new IntentFilter("android.media.VOLUME_CHANGED_ACTION"); intentFilter.addAction("android.net.wifi.WIFI_STATE_CHANGED"); registerReceiver(new TestBroadcastReceiver(), intentFilter); }
轉成apk,安裝到手機或雷電模擬器測試看看
獲取更多Wifi狀態訊息: 修改TestBroadcastReceiver.java public void onReceive(Context context, Intent intent) { … else if(intent.getAction().equals("android.net.wifi.WIFI_STATE_CHANGED")) { String msg =""; int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN); switch (wifiState) { case WifiManager.WIFI_STATE_ENABLED: msg = "wifi已開啟"; break; case WifiManager.WIFI_STATE_ENABLING: msg = "wifi開啟中..."; break; case WifiManager.WIFI_STATE_DISABLED: msg = "wifi已關閉"; break; case WifiManager.WIFI_STATE_DISABLING: msg = "wifi關閉中..."; break; } Toast.makeText(context, "Wifi狀態改變:" + msg, Toast.LENGTH_SHORT).show();
轉成apk,安裝到手機或雷電模擬器測試看看
自行練習:藍牙(需安裝至手機測試) 偵測藍牙狀態變動之系統廣播名稱 判斷藍牙狀態 需允許權限 android.bluetooth.adapter.action.STATE_CHANGED 判斷藍牙狀態 int BluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); BluetoothAdapter.STATE_TURNING_ON:"藍牙開啟中... " BluetoothAdapter.STATE_ON:"藍牙已開啟" BluetoothAdapter.STATE_TURNING_OFF:"藍牙關閉中..." BluetoothAdapter.STATE_OFF:"藍牙已關閉" 需允許權限 <uses-permission android:name="android.permission.BLUETOOTH" />
3.自訂廣播 改成LinearLayout,加入3個按鈕(註冊/發送/解除),在onClick屬性分別填入reg, send, unreg
修改TestBroadcastReceiver.java public void onReceive(Context context, Intent intent) { … else{ Toast.makeText(context, intent.getAction(), Toast.LENGTH_SHORT).show(); }
修改MainActivity.java public class MainActivity extends AppCompatActivity { TestBroadcastReceiver receiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); receiver = new TestBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter("android.media.VOLUME_CHANGED_ACTION"); intentFilter.addAction("android.net.wifi.WIFI_STATE_CHANGED"); registerReceiver(receiver, intentFilter); }
修改MainActivity.java (註冊與發送) public void reg(View v){ IntentFilter intentFilter = new IntentFilter("tw.edu.pu.csim.tcyang.broadcast.MyRegister"); registerReceiver(receiver, intentFilter); } public void send(View v){ Intent intent =new Intent("tw.edu.pu.csim.tcyang.broadcast.MyRegister"); sendBroadcast(intent); 前面是此專案的package 自訂名稱
修改MainActivity.java (解除) public void unreg(View v){ try{ unregisterReceiver(receiver); Toast.makeText(MainActivity.this,"廣播接收器已解除", Toast.LENGTH_SHORT).show(); } catch(Exception exception) { Toast.makeText(MainActivity.this,"先前已經解除廣播接收器了喔", Toast.LENGTH_SHORT).show(); 轉成apk,安裝後測試模擬看看
4.判斷是否連上網路的廣播 開啟新的專案,例如NetworkCheck Empty activity AndroidManifest.xml加入以下存取權限: <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 新增Java Class,例如命名為NetworkCheckReceiver,繼承BroadcastReceiver
NetworkCheckReceiver.java public void onReceive(Context context, Intent intent) { //判斷是否有網路連線 ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); String status = null; if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) { status = "網路連線中:"; if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) { status += "Wifi已啟用"; } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE){ status += "行動數據已啟用"; } else{ status = "無網路連線"; Toast.makeText(context, status, Toast.LENGTH_LONG).show();
MainActivity.java(註冊系統廣播) protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"); registerReceiver(new NetworkCheckReceiver(), intentFilter); }