扩展BroadcastReceiver的android getSystemService错误

时间:2017-03-28 12:09:39

标签: android android-broadcastreceiver

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

我想从getCellDatapublic void onReceive获取返回字符串。 getCellData()中的 getSystemService 出错。我能做什么。感谢。

2 个答案:

答案 0 :(得分:1)

getSystemService()Context上的一种方法。 BroadcastReceiver不会从Context继承。但是,您会将Context传递给onReceive()。因此,请在getSystemService()参数上调用Context

答案 1 :(得分:0)

由于您在ContextMenu contextMenu = new ContextMenu(); MenuItem mItem1 = new MenuItem() { Header = "mItem1" }; MenuItem mItem2 = new MenuItem() { Header = "mItem2" }; mItem1.Click += (ss, ee) => { mItem2.IsEnabled = false; }; mItem2.Click += (ss, ee) => { }; 方法中传递上下文,只需使用

之类的上下文调用它

onReceive()