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