在Android中获取当前位置
我知道这是一个常见的问题,已经被回答了很多次。。。但我正在寻找一个具体问题的答案。 我已经写了一个方法,需要返回当前的lat/long作为字符串。代码如下所示:在Android中获取当前位置,android,locationmanager,android-context,Android,Locationmanager,Android Context,我知道这是一个常见的问题,已经被回答了很多次。。。但我正在寻找一个具体问题的答案。 我已经写了一个方法,需要返回当前的lat/long作为字符串。代码如下所示: public class LibraryProvider { public String basicMethod(String string) { String text = string; LocationManager locationManager; String provi
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不允许此.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()。。。。。我想我需要传递上下文…但是如何传递???您需要一个上下文来调用getSystemService(…)。
实际上,您是从“this”调用它的,它不是一个具有上下文的类。尝试在自定义类构造函数中获取活动上下文引用并使用它
public class LibraryProvider {
private Context context;
public LibraryProvider(Context context){
this.context=context;
}
}
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
试试这个
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();
}
};
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
试试这个
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();
}
};
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
请参见上的此链接,您需要的是活动上下文引用而不是类。您的类未扩展任何活动类上下文是错误的。请使用活动扩展您的类。我实际上正在尝试在另一个类的服务中使用DexClassLoader加载此.apkapp@anantsophia,如果ans正在解决您可以接受的问题,很高兴为您提供帮助,以便帮助其他人解决相同的问题。请建议对问题的编辑部分进行更改无法使用classLoader newInstance()加载此类,请建议我仅在检查时才需要lat/LONG,不在LocationChange上这将起作用,因为onLocationChanged将始终被触发。当你打开手机时,它也会被触发。