【已解决】android中出错:The method getSystemService(String) is undefined for the type UsbDeviceStateReceiver

【问题】

android程序:

class UsbDeviceStateReceiver extends BroadcastReceiver{
    public void onReceive(Context context, Intent intent) {
		...
   		UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
           
       	...
    }
}

结果出错:

The method getSystemService(String) is undefined for the type UsbDeviceStateReceiver

如图:

The method getSystemService String is undefined for the type

 

【折腾过程】

1.参考:

getSystemService is undefined for the type for GetLocation

去:

把:

UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

改为:

UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);

即可。

 

【总结】

getSystemService是Activity中的context才有的。

所以别的部分直接调用会找不到。

所以要从context中去调用getSystemService即可。



One Thought on “【已解决】android中出错:The method getSystemService(String) is undefined for the type UsbDeviceStateReceiver

  1. 谢谢~~~

发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量