Android BroadcastReceiver + 藍芽狀態監聽Demo
阿新 • • 發佈:2019-02-02
1、BroadcastReceiver基礎
- 動態註冊
- 靜態註冊
- 動態繫結
- 動態登出
- 實現靜態登出【其實就是設定不去響應,並沒有所謂登出】
2、程式碼走起
MyBroadcast:
會在MianActivity中使用程式碼動態註冊它
package weizhengzhou.top.interviewbroadcast.BroadcastDemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* Created by 75213 on 2017/7/11.
* Are you known Broadcast ?
* Broadcast(廣播)是在在元件中傳播資料(Intent)的一種機制。通過Broadcast傳送者和接受者都不需要知道對方是否存在。
* Are you known BroadcastReceiver ?
* BroadcastReceiver是對傳送出來的廣播進行過濾接收並響應的一類元件。
*/
// 使用動態註冊
public class MyBroadcast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context , "動態註冊廣播" , Toast.LENGTH_SHORT).show();
}
}
MyBroadcast2:
會在AndroidManifes.xml中使用j靜態註冊它
package weizhengzhou.top.interviewbroadcast.BroadcastDemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* Created by 75213 on 2017/7/11.
*/
//使用靜態註冊
public class MyBroadcast2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context , "靜態註冊廣播" , Toast.LENGTH_SHORT).show();
}
}
MianActivity:
裡面有部分程式碼是實現藍芽狀態監聽的
package weizhengzhou.top.interviewbroadcast;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import weizhengzhou.top.interviewbroadcast.BluetoothStateBroadcastReceiver.BluetoothStateBroadcastReceiver;
import weizhengzhou.top.interviewbroadcast.BroadcastDemo.MyBroadcast;
import weizhengzhou.top.interviewbroadcast.BroadcastDemo.MyBroadcast2;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button mButton1 , mButton2 , mButton3 , mButton4 , mButton5 , mButton6;
private MyBroadcast mMyBroadcast;
private BluetoothStateBroadcastReceiver mReceiver;
private BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMyBroadcast = new MyBroadcast();
mReceiver = new BluetoothStateBroadcastReceiver();
mButton1 = (Button)findViewById(R.id.testButton1);
mButton2 = (Button)findViewById(R.id.testButton2);
mButton3 = (Button)findViewById(R.id.testButton3);
mButton4 = (Button)findViewById(R.id.testButton4);
mButton5 = (Button)findViewById(R.id.testButton5);
mButton6 = (Button)findViewById(R.id.testButton6);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);
mButton4.setOnClickListener(this);
mButton5.setOnClickListener(this);
mButton6.setOnClickListener(this);
registerReceiver(mReceiver , makeFilters());
//獲取藍芽介面卡例項。如果裝置不支援藍芽則返回null
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//獲取狀態碼
int test = mBluetoothAdapter.getState();
//判斷藍芽狀態
switch (test){
case BluetoothAdapter.STATE_CONNECTED:
Toast.makeText(this , "判斷狀態為連線中" , Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_CONNECTING:
Toast.makeText(this , "判斷狀態為連線" , Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_DISCONNECTED:
Toast.makeText(this , "判斷狀態為斷開" , Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_DISCONNECTING:
Toast.makeText(this , "判斷狀態為斷中" , Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_OFF:
Toast.makeText(this , "關閉" , Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_ON:
Toast.makeText(this , "開啟" , Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
//藍芽監聽需要新增的Action
private IntentFilter makeFilters(){
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED");
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
intentFilter.addAction("android.bluetooth.BluetoothAdapter.STATE_OFF");
intentFilter.addAction("android.bluetooth.BluetoothAdapter.STATE_ON");
return intentFilter;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.testButton1:
//動態註冊
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("weizhengzhou.top");
registerReceiver(mMyBroadcast, intentFilter);
break;
case R.id.testButton2:
//傳送動態註冊的廣播資訊
Intent intent = new Intent("weizhengzhou.top");
sendBroadcast(intent);
break;
case R.id.testButton3:
//傳送靜態註冊的廣播資訊
Intent intent2 = new Intent("weizhengzhou.top.test");
sendBroadcast(intent2);
break;
case R.id.testButton4:
//解除動態繫結的廣播接收者
unregisterReceiver(mMyBroadcast);
break;
case R.id.testButton5:
//實現不響應靜態註冊的廣播
getPackageManager().setComponentEnabledSetting(
new ComponentName("weizhengzhou.top.interviewbroadcast" ,
MyBroadcast2.class.getName()),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
break;
case R.id.testButton6:
//重新啟用靜態廣播接收者
getPackageManager().setComponentEnabledSetting(
new ComponentName("weizhengzhou.top.interviewbroadcast",
MyBroadcast2.class.getName()),
PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
PackageManager.DONT_KILL_APP);
break;
}
}
}
3、藍芽狀態監聽具體程式碼
BluetoothStateBroadcastReceiver:
主要實現藍芽狀態廣播接收
package weizhengzhou.top.interviewbroadcast.BluetoothStateBroadcastReceiver;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* Created by 75213 on 2017/7/11.
* 實現藍芽狀態廣播接收
*/
public class BluetoothStateBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//獲取藍芽裝置例項【如果無裝置連結會返回null,如果在無例項的狀態下呼叫了例項的方法,會報空指標異常】
//主要與藍芽裝置有關係
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(context , "監聽藍芽變化" , Toast.LENGTH_SHORT).show();
switch (action){
case BluetoothDevice.ACTION_ACL_CONNECTED:
Toast.makeText(context , "藍芽裝置:" + device.getName() + "已連結" , Toast.LENGTH_SHORT).show();
break;
case BluetoothDevice.ACTION_ACL_DISCONNECTED:
Toast.makeText(context , "藍芽裝置:" + device.getName() + "斷開連結" , Toast.LENGTH_SHORT).show();
break;
//上面的兩個連結監聽,其實也可以BluetoothAdapter實現,修改狀態碼即可
case BluetoothAdapter.ACTION_STATE_CHANGED:
int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
switch (blueState){
case BluetoothAdapter.STATE_OFF:
Toast.makeText(context , "藍芽關閉", Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_ON:
Toast.makeText(context , "藍芽開啟" , Toast.LENGTH_SHORT).show();
break;
}
break;
default:
break;
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="weizhengzhou.top.interviewbroadcast">
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".BroadcastDemo.MyBroadcast2"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="weizhengzhou.top.test"></action>
</intent-filter>
</receiver>
</application>
</manifest>