Can't call a Bluetooth BroadcastReceiver method in a Service

前端 未结 1 1201
情歌与酒
情歌与酒 2021-01-16 23:51

I have this Service class:

 public class BluetoothService extends Service {

    private static Activity mActivity;

    @Override
    public IBinder onBind(         


        
1条回答
  •  无人共我
    2021-01-17 00:13

    maybe the fact that your receiver is static causing the problem.

    BroadcastReceiver should never be static. it can cause lots of problems.

    other really bad design problem with your code - holding reference to activity inside service, and using it to modify views is really wrong thing to do. it can cause easily to memory leek. the right why to communicate between Service and Activity is by implement android's Messanger, or sending broadcasts between them via BroadcastReceiver.

    if you'll listen to my advice - you won't be have to make your receiver static (I guess you've made it static only because you are using the mActivity static instance inside) and I'm pretty sure it will solve your problem

    you can read about Messanger here: http://developer.android.com/reference/android/os/Messenger.html

    sure you'll find lots of usage examples in the net.

    example of broadcasting updates to the activity from service:

    public class MyService extends Service {
    
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
    
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        this.registerReceiver(bluetoothReceiver, intentFilter);
    }
    
    @Override
    public void onDestroy() {
        if (bluetoothReceiver != null) {
            this.unregisterReceiver(bluetoothReceiver);
        }
        super.onDestroy();
    }
    
    public BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                updateUIWithNewState(state);
            }
        }
    };
    
    protected void updateUIWithNewState(int state) {
        Intent intent = new Intent("serviceUpdateReceivedAction");
        intent.putExtra("state", state);
        sendBroadcast(intent);
    }
    
    }
    

    and that's the activity:

    public class MyActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
    
        registerReceiver(mServiceUpdatesReceiver, new IntentFilter("serviceUpdateReceivedAction"));
    }
    
    @Override
    protected void onPause() {
        unregisterReceiver(mServiceUpdatesReceiver);
        super.onPause();
    }
    
    private BroadcastReceiver mServiceUpdatesReceiver = new BroadcastReceiver() {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            int state = intent.getIntExtra("state", -1);
            // do what ever you want in the UI according to the state
        }
    };
    }
    

    0 讨论(0)
提交回复
热议问题