android - 如何在非 Activity 类 (LocationManager) 中使用 getSystemService?

标签 android

我无法将主要 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.java

package 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;
    }
}

最佳答案

你需要将你的上下文传递给你的 fyl 类..
一种解决方案是为您的 fyl 类创建一个这样的构造函数:

public class fyl {
 Context mContext;
 public fyl(Context mContext) {
       this.mContext = mContext;
 }

 public Location getLocation() {
       --
       locationManager = (LocationManager)mContext.getSystemService(context);

       --
 }
}

因此,在您的 Activity 类中,在 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);
    }
}

关于android - 如何在非 Activity 类 (LocationManager) 中使用 getSystemService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4870667/

相关文章:

java - 在 Android 中使用图像检测裁剪人脸

android - ListView 中的涟漪效应奇怪行为

android - 如何修复 Kotlin Realm 扩展中的 "Feature is not part of the schema for this Realm"

android - 为跨平台 OpenGL ES 2.0+ 引擎选择语言和设计 - iOS 和 Android

android - 调用 context.getResources() 返回 null

android - 如何通过浏览器链接启动 Android 应用程序?

android - 以编程方式更改 ImageButton 的背景

java - UI automator/有人遇到过 UI automator 的问题吗? (无法点击元素)

android - 我无法构建我的 APK 帮助我修复它

java - 如何缓存来自 Web 服务器的 okHTTP 响应?