如何在非活动类(LocationManager)中使用getSystemService ?

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

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

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


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


--
}
}

因此,在你的活动类中,在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);
}
}

我解决这个问题的一种方法是为实例创建一个静态类。我在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);

你可以这样做:

getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);

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

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

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

Worker(Context context, WorkerParameters workerParams)

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

mContext.getSystenService(Context.ACTIVITY_SERVICE)
< p >使用这个 在活动:< / p >
private Context context = this;


........
if(Utils.isInternetAvailable(context){
Utils.showToast(context, "toast");
}
..........

效用:

public class Utils {


public static boolean isInternetAvailable(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
}


}

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

requireActivity().getSystemService(LOCATION_SERVICE) as LocationManager