开发中碰到的问题:The method getSystemService(String) is undefined for the type CourseFragment

来源:互联网 发布:做淘宝销售什么意思 编辑:程序博客网 时间:2024/06/06 15:36

【问题】

android程序:

?
1
2
3
4
5
6
7
8
class UsbDeviceStateReceiver extends BroadcastReceiver{
    publicvoid 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

去:

把:

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

改为:

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

即可。

【总结】

getSystemService是Activity中的context才有的。

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

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

0 0