知乎专栏 |
设置 filter
<receiver android:name="com.netkiller.conference.receiver.StaticBroadcastReceiver" android:enabled="true" android:exported="true"> <intent-filter android:priority="1000"> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.ACTION_BATTERY_CHANGED" /> <action android:name="android.intent.action.ACTION_BATTERY_LOW" /> <action android:name="android.intent.action.ACTION_BATTERY_OKAY" /> <action android:name="android.intent.action.ACTION_POWER_CONNECTED" /> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /> <action android:name="android.intent.action.DOWNLOAD_COMPLETE" /> <action android:name="android.intent.action.ACTION_CLOSE_SYSTEM_DIALOGS" /> <action android:name="android.bluetooth.device.action.ACL_CONNECTED" /> <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" /> <action android:name="android.bluetooth.device.action.STATE_CHANGED" /> <action android:name="android.bluetooth.device.action.BOND_STATE_CHANGED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver>
处理蓝牙事件
package com.netkiller.conference.receiver import android.app.DownloadManager import android.bluetooth.BluetoothAdapter import android.bluetooth.BluetoothDevice import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.util.Log import android.widget.Toast import com.netkiller.conference.ai.aigc.AigcSpeech class StaticBroadcastReceiver : BroadcastReceiver() { // private val cache = Cache() private val aigcSpeech: AigcSpeech = AigcSpeech.getInstance() private val TAG: String = StaticBroadcastReceiver::class.java.simpleName override fun onReceive(context: Context, intent: Intent) { val action = intent.action Log.d(TAG, "Static broadcast Action: " + action) val device = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE) try { when (action) { Intent.ACTION_BOOT_COMPLETED -> { Log.d(TAG, "自启动了 !!!!!") // intent = Intent(context, MainActivity::class.java) // 要启动的Activity // //1.如果自启动APP,参数为需要自动启动的应用包名 // //Intent intent = getPackageManager().getLaunchIntentForPackage(packageName); // //这句话必须加上才能开机自动运行app的界面 // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) // //2.如果自启动Activity // context.startActivity(intent) // aigcSpeech.say("电量发生改变"); Log.e(TAG, "BatteryBroadcastReceiver --> ACTION_BATTERY_CHANGED") } Intent.ACTION_BATTERY_CHANGED -> Log.e(TAG, "BatteryBroadcastReceiver --> ACTION_BATTERY_CHANGED") Intent.ACTION_BATTERY_LOW -> // aigcSpeech.say("电量低"); Log.e(TAG, "BatteryBroadcastReceiver --> ACTION_BATTERY_LOW") Intent.ACTION_BATTERY_OKAY -> // aigcSpeech.say("电量充满"); Log.e(TAG, "BatteryBroadcastReceiver --> ACTION_BATTERY_OKAY") Intent.ACTION_POWER_CONNECTED -> { aigcSpeech.speaking("接通电源") Log.e(TAG, "BatteryBroadcastReceiver --> ACTION_POWER_CONNECTED") } Intent.ACTION_POWER_DISCONNECTED -> { aigcSpeech.speaking("拔出电源") Log.e(TAG, "BatteryBroadcastReceiver --> ACTION_POWER_DISCONNECTED") } DownloadManager.ACTION_DOWNLOAD_COMPLETE -> { // aigcSpeech.say("文件下载完成"); val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0) // cache.setDownloadCache(id) } // Intent.ACTION_CLOSE_SYSTEM_DIALOGS -> // 监听home键 //// String reason = intent.getStringExtra(SYSTEM_REASON); // Log.d(TAG, "Home") BluetoothDevice.ACTION_ACL_CONNECTED -> { device?.let { Toast.makeText(context, "蓝牙已连接: ${it.name}", Toast.LENGTH_SHORT).show() } aigcSpeech.speaking("蓝牙麦克风连接") Log.d(TAG, "Bluetooth connected") } BluetoothDevice.ACTION_ACL_DISCONNECTED -> { device?.let { Toast.makeText(context, "蓝牙已断开: ${it.name}", Toast.LENGTH_SHORT).show() } aigcSpeech.speaking("蓝牙麦克风断开") Log.d(TAG, "Bluetooth disconnected") } BluetoothDevice.ACTION_BOND_STATE_CHANGED -> { val state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR) val previousState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR) if (state == BluetoothDevice.BOND_BONDED) { Toast.makeText(context, "设备已配对", Toast.LENGTH_SHORT).show() } else if (state == BluetoothDevice.BOND_NONE) { Toast.makeText(context, "设备取消配对", Toast.LENGTH_SHORT).show() } } BluetoothAdapter.ACTION_STATE_CHANGED -> { val state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR) when (state) { BluetoothAdapter.STATE_ON -> Log.d(TAG, "蓝牙已开启") BluetoothAdapter.STATE_OFF -> Log.d(TAG, "蓝牙已关闭") } } // "main.screen" -> context.startActivity(Intent(context, MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) } } catch (e: Exception) { Log.d(TAG, e.toString()) } } }
解决 main.story 自定义广播接收不到的问题,解决方法设置包 setPackage 即可
intent.setPackage(context.getPackageName());
public static void broadcastStoryPageable(Integer page, String image, String audio, String story) { Context context = ContextHolder.getContext(); Intent intent = new Intent(); intent.setAction("main.story"); intent.putExtra("page", page); intent.putExtra("image", image); intent.putExtra("audio", audio); intent.putExtra("story", story); intent.setPackage(context.getPackageName()); context.sendBroadcast(intent); }