我有麻烦卸载任务从主要活动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;
    }
}

当前回答

对于一些非活动类,比如Worker,在公共构造函数中已经给了一个Context对象。

Worker(Context context, WorkerParameters workerParams)

您可以使用它,例如,将它保存到类中的私有上下文变量(例如,mContext),然后,例如

mContext.getSystenService(Context.ACTIVITY_SERVICE)

其他回答

我不知道这是否有用,但我这样做了:

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

对于一些非活动类,比如Worker,在公共构造函数中已经给了一个Context对象。

Worker(Context context, WorkerParameters workerParams)

您可以使用它,例如,将它保存到类中的私有上下文变量(例如,mContext),然后,例如

mContext.getSystenService(Context.ACTIVITY_SERVICE)

如果你想在一个片段中得到它,这将在kotlin工作:

requireActivity().getSystemService(LOCATION_SERVICE) as LocationManager

你可以这样做:

getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);

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

Config.java

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);