为什么在BroadcastReceiver中用Toast报错?
高洛峰
高洛峰 2016-10-28 16:37:58
0
1
502
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教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(1)
三叔

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

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

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


热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!