我有一个进度条,它一直运行直到我连接到ble。-
@Override
public void Progress() {
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (progressDialog == null) {
progressDialog = ProgressDialog.show(activity, getString(R.string.wait), getResources().getString(R.string.connecting), true, false);
}
progress.setVisibility(View.VISIBLE);
container.setVisibility(View.GONE);
}
});
}
当进程正在运行时,我的意思是当显示进程对话框时,如果设置中的蓝牙被禁用,我需要检测它并停止进程并关闭对话框。如何在
run()
中执行此操作。 最佳答案
为蓝牙创建一个宽频广播接收器。
因此,当蓝牙启用和禁用时,将执行此操作。
您必须注册以下操作。
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
在你的宽频接收器中,你必须像下面这样修改你的代码。
@Override
public void onReceive(Context context, Intent intent) {
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
boolean bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)
== BluetoothAdapter.STATE_ON;
//when bluetooth enabled
if(bluetoothState) {
//hide your progress bar here
}
}
}
如果BluetoothState为true,则表示蓝牙已启用,而false表示已禁用。