获取Android中的当前位置

时间:2015-05-06 11:12:46

标签: android locationmanager android-context

我知道这是一个常见问题并且已经多次回答......但我正在寻找特定问题的答案。 我写了一个方法,需要将当前的lat / long作为字符串返回。代码如下:

public class LibraryProvider {
    public String basicMethod(String string) {
        String text = string;
        LocationManager locationManager;
        String provider;
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);
        if (location != null) {
            text = "Provider " + provider + " has been selected.";
            int lat = (int) (location.getLatitude());
            int lng = (int) (location.getLongitude());
            text = text + "\n" + "lat: " + String.valueOf(lat);
            text = text + "\n" + "lng: " + String.valueOf(lng);
        } else {
            text = "Location not available";
        }

        return text;
    }
}

然而,android studio不允许this.get系统服务:

  

无法解析方法getSystemService(java.lang.String)

我是android的新手,并不清楚上下文和意图......我认为这个问题与它有关。

(编辑)的 我用来加载上面的类的代码在

下面
private final class ServiceHandler extends android.os.Handler {

    public ServiceHandler(Looper looper){
        super(looper);
    }

    public void handleMessage(Message msg){
        dynamicClassLoader();
    }

    String text;

    private void dynamicClassLoader(){

        final String apkFile =Environment.getExternalStorageDirectory().getAbsolutePath()+"/Download/apkFile.apk";
        String className = "com.va.android.level2.lib.LibraryProvider";
        final File optimisedDexOutputPath = getDir("outdex", 0);
        DexClassLoader classLoader = new DexClassLoader(apkFile,optimisedDexOutputPath.getAbsolutePath(),null,getClassLoader());
        try{
            Class loadedClass = classLoader.loadClass(className);
          //  LibraryInterface lib = (LibraryInterface) loadedClass.newInstance();
          //  lib.basicMethod("hello");
            Object obj =(Object)loadedClass.newInstance();
            Method method = loadedClass.getMethod("basicMethod", String.class);
            text = (String) method.invoke(obj,"added method");
            showOutput(text);

        } catch (Exception e){
            e.printStackTrace();
        }

    }

    private void showOutput(final String str){
        mServiceHandler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MyService.this,str, Toast.LENGTH_LONG).show();
            }
        });
    }
}

之前它正在运行,但现在它在loadedClass.newInstance()中引发了一些异常; .....我想我需要传递上下文......但是如何?

4 个答案:

答案 0 :(得分:1)

尝试在自定义类构造函数中获取活动上下文引用并使用它;

public class LibraryProvider {
  private Context context;
  public LibraryProvider(Context context){
      this.context=context;
  }
}

locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

答案 1 :(得分:0)

您需要一个Context来调用getSystemService(...)。 实际上你是从#34;这个"这不是一个有上下文的类。

答案 2 :(得分:0)

试试这个

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
              // Called when a new location is found by the network location provider.
              Log.d("DEBUG",location.toString());
              double lat = location.getLatitude();
              double lon = location.getLongitude();
            }
        };

答案 3 :(得分:0)

试试这个

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

how to get current location in Android

上查看此链接