0
我有下面的廣播接收器,並顯示它包含大量的註冊操作。我的問題是如何處理包含大量操作的廣播接收器的推薦方式?我應該把它們分開放在一個單獨的文件中嗎?或將它們放入服務中?什麼是利用廣播接收器的推薦方式
請指教。
更新:
在情況下,我具有到接收器在另一文件分開,如何接收來自它的更新的通知,以及如何可以基於更新後的通知執行一個動作,ö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個。您可以使用廣播接收通知服務基礎。 –