使用广播接收器的推荐方法是什么?

时间:2015-08-28 08:53:36

标签: android android-intent service broadcastreceiver android-broadcast

我有以下广播的接收器,并且如图所示它包含许多注册的动作。我的问题是,处理包含大量行动的广泛接收器的推荐方法是什么? 我应该在一个单独的文件中分开它们吗?或者将它们投入服务?

请告知。

更新

如果我必须在另一个文件中分隔接收器,我如何从它接收更新的通知,以及我如何根据更新的通知执行操作,ö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;
        }
    }
};

1 个答案:

答案 0 :(得分:0)

最好将其放入单独的文件中。为每个创建唯一方法。

但如果在特定行动中有长期任务,那么它应该在服务或线程上。