简体   繁体   English

将数据传递到已经具有来自活动的意图过滤器的广播接收器

[英]pass data to broadcast receiver that already has intent filter coming from activity

when the phone is connected to the bluetooth in the car. 当手机连接到汽车中的蓝牙时。 I want my app to switch on automatically. 我希望我的应用程序自动打开。 to do this i have to save the paired car bluetooth device name to a string. 为此,我必须将配对的汽车蓝牙设备名称保存到字符串中。 then when the phones bluetooth is connected to something. 然后当手机蓝牙连接到某物时。 I have to check if its the car. 我必须检查它的车。 If it is i want to start a service. 如果是我想开始一项服务。

I'm having difficulty passing the string that contains the bluetooth car device name to the receiver, as my receiver is already receiving a intent filter to listen out for ACTION_ACL_CONNECTED. 我很难将包含蓝牙汽车设备名称的字符串传递给接收器,因为我的接收器已经在接收一个意图过滤器以侦听ACTION_ACL_CONNECTED。 is it possible to send a intent & an intent filter to the same receiver. 是否可以向同一接收者发送意图和意图过滤器。

How can I can send the btdeviceName string from the activity to the receiver in this case. 在这种情况下,如何将btdeviceName字符串从活动发送到接收者。

Main Activity 主要活动

 private void addDrawerItems() {

    final BroadcastReceiver bluetoothBroadcast = new BluetoothReceiver();
    final IntentFilter blueToothFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);


    final Intent btbroadcastIntent = new Intent(this, BluetoothReceiver.class);
    btbroadcastIntent.putExtra("btDeviceName", mPairedBluetoothDevice);


    String[] osArray = {"Bluetooth Auto Start", "Reply to Calls", "Reply to sms", "Customise Message"};

    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, osArray);


    if (mIsPremiumUser) {
        mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    } else {
        mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
    }

    mDrawerList.setAdapter(mAdapter);


    mDrawerList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {


            if (position == 0) {
                Toast.makeText(getApplicationContext(), "blue", Toast.LENGTH_LONG).show();
                showBluetoothDialog();
            }
            return true;
        }
    });


    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            CheckedTextView ctv = (CheckedTextView) view;

            if (!mIsPremiumUser) {
                Toast.makeText(getApplication(), "Upgrade", Toast.LENGTH_LONG).show();
                return;
            }


            switch (position) {

                case 0:

                    if (ctv.isChecked()) {

                        if (!isblueToothRegistered) {
                            registerReceiver(bluetoothBroadcast, blueToothFilter);
                            sendBroadcast(btbroadcastIntent);
                            isblueToothRegistered = true;
                        }

                    } else {
                        if (isblueToothRegistered) {
                            unregisterReceiver(bluetoothBroadcast);
                            isblueToothRegistered = false;
                        }
                    }

                    break;

BluetoothReceiver 蓝牙接收器

public class BluetoothReceiver extends BroadcastReceiver {

private MainActivity ma;
private String pairedDevice;


public void onReceive(Context context, Intent intent) {


    Toast.makeText(context, "Receieved", Toast.LENGTH_LONG).show();
    String action = intent.getAction();

    pairedDevice = intent.getStringExtra("btDeviceName");
    Toast.makeText(context, pairedDevice + "2", Toast.LENGTH_LONG).show();

    final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);


    if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
        Toast.makeText(context, "Bluetooth Connected", Toast.LENGTH_LONG).show();


        if (device.getName().equals(pairedDevice)) {
            Toast.makeText(context, device.getName() + " 1", Toast.LENGTH_LONG).show();
        }


    } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
        Toast.makeText(context, "Bluetooth Disconnected", Toast.LENGTH_LONG).show();
    }


}

} }

An IntentFilter can have multiple actions. 一个IntentFilter可以有多个动作。 So start by creating your own custom action name to listen for and add it to the blueToothFilter . 因此,首先创建您自己的自定义操作名称来侦听并将其添加到blueToothFilter

blueToothFilter.addAction("my.custom.action");

Once you register the bluetoothBroadcast receiver with this IntentFilter it will now receive calls for both actions. 使用此IntentFilter注册bluetoothBroadcast接收器后,它现在将接收这两个操作的调用。 Add another condition in onReceive to handle your new custom action. onReceive添加另一个条件以处理新的自定义操作。

Finally in your Activity send a broadcast with your custom action and the device name when ready. 最后,在“ Activity发送一个广播,其中包含您的自定义操作和准备好的设备名称。

Intent intent = new Intent()
        .setAction("my.custom.action")
        .putExtra("btDeviceName", mPairedBluetoothDevice);

sendBroadcast(intent);

UPDATE 更新

I now understand that you want both the BluetoothDevice device and String pairedDevice in a single call to onReceive() . 我现在知道,您希望在一次调用onReceive()同时使用BluetoothDevice deviceString pairedDevice That is not possible since those variables are taken from separate actions and each action calls onReceive() once. 这是不可能的,因为这些变量是从单独的操作中提取的,并且每个操作都调用一次onReceive()

To fix this you can change the BluetoothReceiver to be an inner class of your Activity so you can keep a reference to the data you need there. 要解决此问题,您可以将BluetoothReceiver更改为Activity的内部类,以便在那里保留对所需数据的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM