问题
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