0

I want to use getSystemService in a non-Activity class but when i do I get some error. here is the code below:

double latitude,longitude;
android.location.Location l;
static LocationManager lm;
Toast.makeText(context, "Before", Toast.LENGTH_SHORT).show();
lm = (LocationManager)getSystemService(LOCATION_SERVICE);
l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
latitude= l.getLatitude();
longitude = l.getLongitude();

String po="after";
Toast.makeText(context, po1, Toast.LENGTH_LONG).show();

I do not want to use any other activity class. Any help would be appreciated. All this work is done in a non activity class method. here is the full code of broadcast reciever

public class SMSReciever extends BroadcastReceiver {

android.location.Location l;
static LocationManager lm;
SmsMessage[] msgs = null;
String str = "";
double lat,lng;
String request,ipaddress="",originatingno;

public void onReceive (Context context, Intent intent) {
    // Parse the SMS.
    Bundle bundle = intent.getExtras();

    if(bundle != null)
    {
        // Retrieve the SMS.
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for (int i=0; i<msgs.length; i++)
        {
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
            request=msgs[i].getMessageBody().toString();
            ipaddress=request.substring(4);
            originatingno= msgs[i].getOriginatingAddress();


        }
        if(request.startsWith("fr"))
        {


            sabkuch db =new sabkuch(context); 
            db.open();
            long id = db.insertContact(originatingno,"shubh");
            db.close();

        }
        if(request.startsWith("lr"))
        {
            double latitude,longitude;
             Toast.makeText(context, "Before", Toast.LENGTH_SHORT).show();

             lm = (LocationManager)context.getSystemService(context.LOCATION_SERVICE);
              //String provider = LocationManager.GPS_PROVIDER;
             l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            latitude= l.getLatitude();
             longitude = l.getLongitude();

             String po="Shubham's location: " +
                    " Latitude: "+String.valueOf(latitude)+
                    " Longitude: "+String.valueOf(longitude);

           Toast.makeText(context, po, Toast.LENGTH_LONG).show();

        }


    }}

}

shubham
  • 25
  • 7
  • 2
    see this http://stackoverflow.com/questions/4870667/how-can-i-use-getsystemservice-in-a-non-activity-class-locationmanager – nobalG Jul 21 '14 at 06:08
  • 1
    send the context of calling activity to the constructor of the non Activity class – Arash GM Jul 21 '14 at 06:08

1 Answers1

0

Try this from Activity send Context to non activity class and with that context you can use getSystemService.

lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

EDIT :

public class SMSReciever extends BroadcastReceiver {

android.location.Location l;
LocationManager lm;
SmsMessage[] msgs = null;
String str = "";
double lat,lng;
String request,ipaddress="",originatingno;

public void onReceive (Context context, Intent intent) {
    // Parse the SMS.
    Bundle bundle = intent.getExtras();

    if(bundle != null)
    {
        // Retrieve the SMS.
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for (int i=0; i<msgs.length; i++)
        {
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
            request=msgs[i].getMessageBody().toString();
            ipaddress=request.substring(4);
            originatingno= msgs[i].getOriginatingAddress();


        }
        if(request.startsWith("fr"))
        {


            sabkuch db =new sabkuch(context); 
            db.open();
            long id = db.insertContact(originatingno,"shubh");
            db.close();

        }
        if(request.startsWith("lr"))
        {
            double latitude,longitude;
             Toast.makeText(context, "Before", Toast.LENGTH_SHORT).show();

             lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
              //String provider = LocationManager.GPS_PROVIDER;
             l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            latitude= l.getLatitude();
             longitude = l.getLongitude();

             String po="Shubham's location: " +
                    " Latitude: "+String.valueOf(latitude)+
                    " Longitude: "+String.valueOf(longitude);

           Toast.makeText(context, po, Toast.LENGTH_LONG).show();

        }
    }
  }
}
Kaushik
  • 6,150
  • 5
  • 39
  • 54
  • I am not using activity. Actually I am using a broadcast reciever which i have made inside a non activity class, so whenever a message will arrive it will generate the location of the phone and send it back. So how to send context?? – shubham Jul 21 '14 at 06:15
  • 1
    In the BroadcastReceiver's public void onReceive(Context context, Intent intent) method you can use the context parameter to access the system services like described by kaushik. In utility classes, that need to access system services you should pass the Context object (the Activity itself via this) as a method or constructor parameter. – Patrick Leitermann Jul 21 '14 at 06:21
  • @shubham : u have used `context` to show `Toast`. So, use the same `context` for `getSystemService`. In `BroadcastReceiver's` `onReceive(Context context, Intent intent)` u'll get the `context` as [**ptk93**](http://stackoverflow.com/users/3649498/ptk93) mentioned. – Kaushik Jul 21 '14 at 06:27
  • when i use that context it shows LOCATION_SERVICE cannot be resolved to a variable.. And when i use lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); it does not show error there but when i receive some message it shows null pointer exception in logcat – shubham Jul 21 '14 at 06:38
  • lm = (LocationManager)context.getSystemService(`context`.LOCATION_SERVICE); instead use lm = (LocationManager)context.getSystemService(`Context`.LOCATION_SERVICE); – Kaushik Jul 21 '14 at 06:48
  • @kaushik do something buddy its my project i have to submit – shubham Jul 21 '14 at 06:53
  • don't know what happened.But what I have written as answer that works for me. try to copy that line from my answer and replace that in your code. – Kaushik Jul 21 '14 at 06:57
  • can u send me your code means full code so that I could take some hint – shubham Jul 21 '14 at 09:25
  • @shubham : you can take a look at [using the LOCATION_SERVICE from a Thread](http://stackoverflow.com/questions/17914170/using-the-location-service-from-a-thread) – Kaushik Jul 21 '14 at 12:50