什么是利用广播接收器的推荐方式
问题描述:
我有下面的广播接收器,并显示它包含大量的注册操作。我的问题是如何处理包含大量操作的广播接收器的推荐方式?我应该把它们分开放在一个单独的文件中吗?或将它们放入服务中?什么是利用广播接收器的推荐方式
请指教。
更新:
在情况下,我具有到接收器在另一文件分开,如何接收来自它的更新的通知,以及如何可以基于更新后的通知执行一个动作,ölike禁用一个例子中的“主要活动”按钮?
代码:
private final BroadcastReceiver btReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
tvStatusLabel.setVisibility(TextView.VISIBLE);
String action = intent.getAction();
switch (action) {
//ACTION_FOUND
case BluetoothDevice.ACTION_FOUND:
Log.d(TAG, LogAnd.show("onReceive", "BluetoothDevice.ACTION_FOUND"));
BluetoothDevice stateExtDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
setBTDeviceExtrs(stateExtDevice);
newDeviceFound = true;
break;
case BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED:
int prevConnState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE, BluetoothDevice.ERROR);
switch (prevConnState) {
case BluetoothAdapter.STATE_DISCONNECTED:
Log.d(TAG, LogAnd.show("onReceive", "prevConnState: DISCONNECTED"));
tvStatus.setText("prevConnState:DISCONNECTED");
break;
case BluetoothAdapter.STATE_CONNECTING:
Log.d(TAG, LogAnd.show("onReceive", "prevConnState: CONNECTING"));
tvStatus.setText("prevConnState:CONNECTING");
break;
case BluetoothAdapter.STATE_CONNECTED:
Log.d(TAG, LogAnd.show("onReceive", "prevConnState: CONNECTED"));
tvStatus.setText("prevConnState:CONNECTED");
break;
case BluetoothAdapter.STATE_DISCONNECTING:
Log.d(TAG, LogAnd.show("onReceive", "prevConnState: DISCONNECTING"));
tvStatus.setText("prevConnState:DISCONNECTING");
break;
default:
Log.wtf(TAG, LogAnd.show("onReceive", "prevConnState: UNHANDELED CASE"));
tvStatus.setText("prevConnState: UNHANDELED CASE");
break;
}
int currConnState = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, BluetoothDevice.ERROR);
switch (currConnState) {
case BluetoothAdapter.STATE_DISCONNECTED:
Log.d(TAG, LogAnd.show("onReceive", "currConnState: DISCONNECTED"));
tvStatus.setText("currConnState:DISCONNECTED");
break;
case BluetoothAdapter.STATE_CONNECTING:
Log.d(TAG, LogAnd.show("onReceive", "currConnState: CONNECTING"));
tvStatus.setText("currConnState:CONNECTING");
break;
case BluetoothAdapter.STATE_CONNECTED:
Log.d(TAG, LogAnd.show("onReceive", "currConnState: CONNECTED"));
tvStatus.setText("currConnState:CONNECTED");
break;
case BluetoothAdapter.STATE_DISCONNECTING:
Log.d(TAG, LogAnd.show("onReceive", "currConnState: DISCONNECTING"));
tvStatus.setText("currConnState:DISCONNECTING");
break;
default:
Log.wtf(TAG, LogAnd.show("onReceive", "currConnState: UNHANDELED CASE"));
tvStatus.setText("currConnState: UNHANDELED CASE");
break;
}
if (prevConnState == BluetoothAdapter.STATE_CONNECTING && currConnState == BluetoothAdapter.STATE_CONNECTED) {
Log.d(TAG, LogAnd.show("onReceive", "Profile connected"));
tvStatus.setText("currConnState:Profile connected");
}
if (prevConnState == BluetoothAdapter.STATE_DISCONNECTING && currConnState == BluetoothAdapter.STATE_DISCONNECTED) {
Log.d(TAG, LogAnd.show("onReceive", "Profile disconnected"));
tvStatus.setText("currConnState:Profile disconnected");
}
break;
case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
//ACTION_BOND_STATE_CHANGED
int prevBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR);
switch (prevBondState) {
case BluetoothDevice.BOND_BONDING:
Log.d(TAG, LogAnd.show("onReceive", "prevBondState: BOND_BONDING"));
tvStatus.setText("prevBondState:BOND_BONDING");
break;
case BluetoothDevice.BOND_BONDED:
Log.d(TAG, LogAnd.show("onReceive", "prevBondState: BOND_BONDED"));
tvStatus.setText("prevBondState:BOND_BONDED");
break;
case BluetoothDevice.BOND_NONE:
Log.d(TAG, LogAnd.show("onReceive", "prevBondState: BOND_NONE"));
tvStatus.setText("prevBondState:BOND_NONE");
break;
default:
Log.d(TAG, LogAnd.show("onReceive", "prevBondState: NO_BONDING_STATE"));
tvStatus.setText("prevBondState:NO_BONDING_STATE");
break;
}
int currBondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
switch (currBondState) {
case BluetoothDevice.BOND_BONDING:
Log.d(TAG, LogAnd.show("onReceive", "currBondState: BOND_BONDING"));
tvStatus.setText("currBondState:BOND_BONDING");
break;
case BluetoothDevice.BOND_BONDED:
Log.d(TAG, LogAnd.show("onReceive", "currBondState: BOND_BONDED"));
tvStatus.setText("currBondState:BOND_BONDED");
break;
case BluetoothDevice.BOND_NONE:
Log.d(TAG, LogAnd.show("onReceive", "currBondState: BOND_NONE"));
tvStatus.setText("currBondState:BOND_NONE");
break;
default:
Log.wtf(TAG, LogAnd.show("onReceive", "currBondState: NO_BONDING_STATE"));
tvStatus.setText("currBondState:NO_BONDING_STATE");
break;
}
if ((prevBondState == BluetoothDevice.BOND_BONDING) && (currBondState == BluetoothDevice.BOND_BONDED)) {
Toast.makeText(getApplicationContext(), "Paired", Toast.LENGTH_SHORT).show();
}
if ((prevBondState == BluetoothDevice.BOND_BONDED) && (currBondState == BluetoothDevice.BOND_NONE)) {
Toast.makeText(getApplicationContext(), "Unpaired", Toast.LENGTH_SHORT).show();
}
break;
//ACTION_DISCOVERY_STARTED
//use AsyncTask to show busy indicator while discovering the adjacent devices.
case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
Log.d(TAG, LogAnd.show("onReceive", "BluetoothAdapter.ACTION_DISCOVERY_STARTED"));
tvStatus.setText("ACTION_DISCOVERY_STARTED");
asyncDis = new AsyncDiscovery();
asyncDis.execute();
break;
//ACTION_DISCOVERY_FINISHED
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
Log.d(TAG, LogAnd.show("onReceive", "BluetoothAdapter.ACTION_DISCOVERY_FINISHED"));
tvStatus.setText("ACTION_DISCOVERY_FINISHED");
break;
//ACTION_STATE_CHANGED
case BluetoothAdapter.ACTION_STATE_CHANGED:
Log.d(TAG, LogAnd.show("onReceive", "BluetoothAdapter.ACTION_STATE_CHANGED"));
final int prevPowState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.ERROR);
switch (prevPowState) {
case BluetoothAdapter.STATE_ON:
Log.d(TAG, LogAnd.show("onReceive", "prevPowState: STATE_ON"));
tvStatus.setText("prevPowState: BT Turned ON.");
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log.d(TAG, LogAnd.show("onReceive", "prevPowState: STATE_TURNING_ON"));
tvStatus.setText("prevPowState: BT is Turning ON.");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.d(TAG, LogAnd.show("onReceive", "prevPowState: STATE_TURNING_OFF"));
tvStatus.setText("prevPowState: BT STATE_TURNING_OFF.");
break;
case BluetoothAdapter.STATE_OFF:
Log.d(TAG, LogAnd.show("onReceive", "prevPowState: STATE_OFF"));
tvStatus.setText("prevPowState: BT STATE_OFF.");
break;
}
final int currPowState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
switch (currPowState) {
case BluetoothAdapter.STATE_ON:
Log.d(TAG, LogAnd.show("onReceive", "currPowState: STATE_ON"));
tvStatus.setText("currPowState: BT Turned ON.");
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log.d(TAG, LogAnd.show("onReceive", "currPowState: STATE_TURNING_ON"));
tvStatus.setText("currPowState: BT is Turning ON.");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.d(TAG, LogAnd.show("onReceive", "currPowState: STATE_TURNING_OFF"));
tvStatus.setText("currPowState: BT STATE_TURNING_OFF.");
break;
case BluetoothAdapter.STATE_OFF:
Log.d(TAG, LogAnd.show("onReceive", "currPowState: STATE_OFF"));
tvStatus.setText("currPowState: BT STATE_OFF.");
break;
}
if ((prevPowState == BluetoothAdapter.STATE_TURNING_ON) && (currPowState == BluetoothAdapter.STATE_ON)) {
Toast.makeText(getApplicationContext(), "BT-Power ON", Toast.LENGTH_SHORT).show();
//initResources();
if (!tbOnOff.isChecked()) {
tbOnOff.setChecked(true);
}
btnDiscover.setEnabled(true);
btnDiscover.setOnClickListener(btnDiscoverListener);
}
if ((prevPowState == BluetoothAdapter.STATE_TURNING_OFF) && (currPowState == BluetoothAdapter.STATE_OFF)) {
Toast.makeText(getApplicationContext(), "BT-Power OFF", Toast.LENGTH_SHORT).show();
//resetResources();
if (tbOnOff.isChecked()) {
tbOnOff.setChecked(false);
}
btnDiscover.setEnabled(false);
btnDiscover.setOnClickListener(null);
adapter.clear();
}
break;
default:
break;
}
}
};
答
更好的,你把单独的一个文件。为每一个创建独特的方法。
但是如果在特定操作中有很长的任务,那么它应该在服务或线程上。
谢谢你的回答。但我可以让广播接收机作为服务吗?我的意思是,我可以克里特服务,并在其中我使用广播接收机,因为我希望一直收到通知?请咨询 – user2121
不,您不能将广播接收机作为服务。您必须为广播Receiver创建两个1类,并为Service创建1个。您可以使用广播接收通知服务基础。 –