android getSystemService error with extends BroadcastReceiver

左心房为你撑大大i 提交于 2019-12-13 09:28:50

问题


I have 2 activities

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;
    }

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.


回答1:


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.




回答2:


Since you are passing context in onReceive() method just call it using that context like

context.getSystemService(Context.TELEPHONY_SERVICE);



来源:https://stackoverflow.com/questions/43069188/android-getsystemservice-error-with-extends-broadcastreceiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!