【问题】
android程序:
1
2
3
4
5
6
7
8
|
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 |
如图:
【折腾过程】
1.参考:
getSystemService is undefined for the type for GetLocation
去:
把:
1
|
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
改为:
1
|
UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
|
即可。
【总结】
getSystemService是Activity中的context才有的。
所以别的部分直接调用会找不到。
所以要从context中去调用getSystemService即可。