Maison  >  Questions et réponses  >  le corps du texte

为什么在BroadcastReceiver中用Toast报错?

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
                Toast.makeText(this, "BroadcastReceiver:  ACTION_GATT_CONNECTED", Toast.LENGTH_SHORT).show();
                mConnected = true;
                updateConnectionState(R.string.connected);
                invalidateOptionsMenu();
            } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
                mConnected = false;
                updateConnectionState(R.string.disconnected);
                invalidateOptionsMenu();
                //clearUI();
            } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
                // Show all the supported services and characteristics on the user interface.
                displayGattServices(mBluetoothLeService.getSupportedGattServices());
            } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
                byte date[]=intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA);
                System.out.println(date+"-------------=-");
                /*if(BlueTooth.mChecksumError==false){
                    displayData(date);    
                }*/

            }
        }
    };

为什么在BroadcastReceiver中我用Toast时候报这个错:The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new BroadcastReceiver(){}, String, int)

高洛峰高洛峰2932 Il y a quelques jours538

répondre à tous(1)je répondrai

  • 三叔

    三叔2016-10-28 16:38:28

    makeText第一参数要求的是Context, 显然, BroadcastReceiver并不是.

    请使用onReceiver方法的第一参数context

    Activity中, makeText可以使用this, 是因为 Activity extends Context了.


    répondre
    0
  • Annulerrépondre