本文介绍了getSystemService未定义为的getLocation类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的GetURL定义您的网络供应商我的位置的方法,您可以发送code以下。

如果我定义了code片在我的主类的活动这工作得很好,但是当我想创建一个独立的类(例如的getLocation类),我不能用getSystemService方法,我在受收到错误( getSystemService未定义用于的getLocation的类型)。有几个关于该主题的作品,但我不完全理解。

这是一个菜鸟的问题,以便考虑到这一点的同时回答:)谢谢你们。

 公共字符串的GetURL(){
    LocationManager locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);
    标准标准=新标准();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    字符串locationProvider = LocationManager.NETWORK_PROVIDER;
    mostRecentLocation = locationManager.getLastKnownLocation(locationProvider);
    如果(mostRecentLocation!= NULL){
        双纬度= mostRecentLocation.getLatitude();
        双LNG = mostRecentLocation.getLongitude();
        currentLocation =纬度:+纬度+LNG:+ LNG;
        Log.e(所在地-------------,currentLocation +);
    }
    返回currentLocation;
}
 

解决方案

该方法<$c$c>getSystemService()属于上下文类。

因此​​,如果要移动的getURL()成为其他地方的实用方法,你需要传递一个上下文的对象,如当前的活动(因为它从上下文)。

例如:
Util.java

 公共静态字符串的getURL(上下文的背景下){
    LocationManager LM =(LocationManager)
        context.getSystemService(Context.LOCATION_SERVICE);

    // ...您现有的code ...
    返回currentLocation;
}
 

MyActivity.java

 公共无效的onCreate(){
    // ...平常的东西......

    字符串URL =的getURL(本);
}
 

I have GetURL method defined to find my location from network provider, you can send the code below.

If I define that code piece in my main class with Activity this works nicely but when I would like to create a seperate class (for instance GetLocation class), I cannot use getSystemService method and I receive error in subject (getSystemService is undefined for the type for GetLocation). There are couple of entries regarding this topic but I don't understand fully.

That's a rookie question so take this into account while answering :) Thanks guys.

public String GetURL () {
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String locationProvider = LocationManager.NETWORK_PROVIDER;
    mostRecentLocation = locationManager.getLastKnownLocation(locationProvider);
    if(mostRecentLocation != null) {
        double lat = mostRecentLocation.getLatitude();
        double lng = mostRecentLocation.getLongitude();
        currentLocation = "Lat: " + lat + " Lng: " + lng;
        Log.e("LOCATION -------------", currentLocation + "");
    }
    return currentLocation;
}
解决方案

The method getSystemService() belongs to the Context class.

Therefore if you want to move getUrl() to become a utility method elsewhere, you need to pass in a Context object, such as the current Activity (as it inherits from Context).

For example:
Util.java

public static String getUrl(Context context) {
    LocationManager lm = (LocationManager) 
        context.getSystemService(Context.LOCATION_SERVICE);

    // ... your existing code ...
    return currentLocation;
}

MyActivity.java

public void onCreate() {
    // ... usual stuff ...

    String url = getUrl(this);
}

这篇关于getSystemService未定义为的getLocation类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 20:00