【问题标题】:using getsystemservice function outside activity [duplicate]在活动之外使用getsystemservice功能[重复]
【发布时间】:2014-07-21 06:04:29
【问题描述】:

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

        }


    }}

}

【问题讨论】:

标签: java android android-activity


【解决方案1】:

尝试从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();

        }
    }
  }
}

【讨论】:

  • 我没有使用活动。实际上,我使用的是我在非活动类中制作的广播接收器,因此每当有消息到达时,它都会生成手机的位置并将其发回。那么如何发送上下文呢??
  • 在 BroadcastReceiver 的 public void onReceive(Context context, Intent intent) 方法中,您可以使用 context 参数访问系统服务,如 kaushik 所述。在需要访问系统服务的实用程序类中,您应该将 Context 对象(通过 this 的 Activity 本身)作为方法或构造函数参数传递。
  • @shubham : 你用context 显示Toast。因此,对getSystemService 使用相同的context。在BroadcastReceiver's onReceive(Context context, Intent intent) 中,你会得到contextptk93 提到的一样。
  • 当我使用该上下文时,它显示 LOCATION_SERVICE 无法解析为变量。当我使用 lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);它在那里没有显示错误,但是当我收到一些消息时,它在 logcat 中显示空指针异常
  • lm = (LocationManager)context.getSystemService(context.LOCATION_SERVICE);而是使用 lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
猜你喜欢
  • 2011-06-19
  • 1970-01-01
  • 1970-01-01
  • 2020-09-11
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
  • 1970-01-01
  • 2011-08-03
相关资源
最近更新 更多