在活动之外使用getsystemservice函数

时间:2014-07-21 06:04:29

标签: java android android-activity

我想在非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();

        }


    }}

}

1 个答案:

答案 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();

        }
    }
  }
}