我想在非Activity类中使用getSystemService,但是当我这样做时,我会收到一些错误。这是下面的代码:
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();
我不想使用任何其他活动类。任何帮助,将不胜感激。所有这些工作都是在非活动类方法中完成的。这是广播接收器的完整代码
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();
}
}}
}
答案 0 :(得分:0)
从Activity
发送Context
到非活动类,然后使用context
,您可以使用getSystemService
。
lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
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();
}
}
}
}