-2

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.

Saurabh
  • 434
  • 1
  • 4
  • 12
salx32
  • 67
  • 1
  • 1
  • 5

2 Answers2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Can you explain how to call getSystemService() on Context parameter. I am new to android. Thank you. – salx32 Mar 29 '17 at 05:48
0

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

context.getSystemService(Context.TELEPHONY_SERVICE);

Abid Khan
  • 2,451
  • 4
  • 22
  • 45