带有扩展BroadcastReceiver的android getSystemService错误 [英] android getSystemService error with extends BroadcastReceiver

查看:78
本文介绍了带有扩展BroadcastReceiver的android getSystemService错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个活动

public class MainActivity extends AppCompatActivity {

    ..............
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    ..............
    ..............
    }
}

public class IncomingSms extends BroadcastReceiver{

    public void onReceive(Context context, Intent intent){
        ..............
        ..............
    }


    public String getCellData() {

        TelephonyManager telephonyManager = (TelephonyManager) ***getSystemService***(Context.TELEPHONY_SERVICE);
        GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();

        ..............
        ..............

        return someString;
    }

我想从 getCellData <中获取返回字符串。 / code>到无效onReceive getCellData()中的 getSystemService 上有错误。我能做什么。谢谢。

I want to get the return string from getCellData to public void onReceive. There is a error on getSystemService in getCellData(). What can I do. Thanks.

推荐答案

getSystemService()是<$ c上的方法$ c>上下文 BroadcastReceiver 不能从 Context 继承。但是,您将 Context 传递给 onReceive()。因此,请在该 Context 参数上调用 getSystemService()

getSystemService() is a method on Context. BroadcastReceiver does not inherit from Context. However, you are passed a Context into onReceive(). So, call getSystemService() on that Context parameter.

这篇关于带有扩展BroadcastReceiver的android getSystemService错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆