为什么在BroadcastReceiver中用Toast报错?
高洛峰
高洛峰 2016-10-28 16:37:58
0
1
540
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)

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

Antworte allen(1)
三叔

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

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

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


Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!