我无法将主要活动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;
}
}
答案 0 :(得分:258)
您需要将您的上下文传递给您的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);
}
}
答案 1 :(得分:44)
你可以这样做:
getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
答案 2 :(得分:14)
我解决这个问题的一种方法是为实例创建一个静态类。我在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);
答案 3 :(得分:0)
我不知道这是否会有所帮助,但我这样做了:
LocationManager locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
答案 4 :(得分:0)
对于某些非活动类,例如Worker,您已经在公共构造函数中获得了Context对象。
Worker(Context context, WorkerParameters workerParams)
您可以使用它,例如将其保存到类中的私有Context变量中(例如mContext
),然后例如
mContext.getSystenService(Context.ACTIVITY_SERVICE)
答案 5 :(得分:0)
使用此 在活动中:
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();
}
}
答案 6 :(得分:0)
如果你想把它放在一个片段中,这可以在 kotlin 中工作:
requireActivity().getSystemService(LOCATION_SERVICE) as LocationManager
答案 7 :(得分:-3)
(LocationManager) ((lmt) new Activity()).getSystemService(context);
试试这段代码。只有在调用此行代码之前已启动活动时,此方法才有效。因为活动必须创建并且正在运行。