为了账号安全,请及时绑定邮箱和手机立即绑定

如何在非活动类(LocationManager)中使用getSystemService?

如何在非活动类(LocationManager)中使用getSystemService?

慕工程0101907 2019-12-18 16:23:48
我无法将主要的Activity OnCreate方法中的任务卸载到另一个类上来进行繁重的工作。当我尝试从非Activity类调用getSystemService时,将引发异常。任何帮助将不胜感激 :)lmt.java:package com.atClass.lmt;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;import android.location.Location;public class lmt extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        fyl lfyl = new fyl();        Location location = lfyl.getLocation();        String latLongString = lfyl.updateWithNewLocation(location);        TextView myLocationText = (TextView)findViewById(R.id.myLocationText);        myLocationText.setText("Your current position is:\n" + latLongString);    }}fyl.javapackage com.atClass.lmt;import android.app.Activity;import android.os.Bundle;import android.location.Location;import android.location.LocationManager;import android.os.Bundle;import android.widget.TextView;import android.content.Context;public class fyl {    public Location getLocation(){        LocationManager locationManager;        String context = Context.LOCATION_SERVICE;        locationManager = (LocationManager)getSystemService(context);        String provider = LocationManager.GPS_PROVIDER;        Location location = locationManager.getLastKnownLocation(provider);        return location;    }    public String updateWithNewLocation(Location location) {        String latLongString;        if (location != null){            double lat = location.getLatitude();            double lng = location.getLongitude();            latLongString = "Lat:" + lat + "\nLong:" + lng;        }else{            latLongString = "No Location";        }        return latLongString;    }}
查看完整描述

3 回答

?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

您需要将上下文传递给fyl类。

一种解决方案是为您的fyl类创建一个类似这样的构造函数:


public class fyl {

 Context mContext;

 public fyl(Context mContext) {

       this.mContext = mContext;

 }


 public Location getLocation() {

       --

       locationManager = (LocationManager)mContext.getSystemService(context);


       --

 }

}

因此,在您的活动类中,在onCreate函数中创建fyl对象,如下所示:


package com.atClass.lmt;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

import android.location.Location;


public class lmt extends Activity {

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);


        fyl lfyl = new fyl(this); //Here the context is passing 


        Location location = lfyl.getLocation();

        String latLongString = lfyl.updateWithNewLocation(location);


        TextView myLocationText = (TextView)findViewById(R.id.myLocationText);

        myLocationText.setText("Your current position is:\n" + latLongString);

    }

}


查看完整回答
反对 回复 2019-12-18
?
森林海

TA贡献2011条经验 获得超2个赞

您可以这样做:


getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);


查看完整回答
反对 回复 2019-12-18
?
米琪卡哇伊

TA贡献1998条经验 获得超6个赞

解决这个问题的一种方法是为实例创建一个静态类。我在AS3中经常使用它,在Android开发中也为我工作得很好。


配置文件


public final class Config {

    public static MyApp context = null;

}

MyApp.java


public class MyApp extends Activity {

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


        Config.context = this;

    }

    ...

}

然后,您可以访问上下文或通过使用 Config.context


LocationManager locationManager;

String context = Context.LOCATION_SERVICE;

locationManager = Config.context.getSystemService(context);


查看完整回答
反对 回复 2019-12-18
  • 3 回答
  • 0 关注
  • 983 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信