如何在非活动类(LocationManager)中使用getSystemService?
我很难将主要活动OnCreate方法中的任务卸载到另一个类上来完成繁重的工作。
当我尝试从非Activity类调用getSystemService时,会引发exception。
任何帮助将不胜感激 :)
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类。
一个解决scheme是为你的fyl
类创build一个这样的构造函数:
public class fyl { Context mContext; public fyl(Context mContext) { this.mContext = mContext; } public Location getLocation() { -- locationManager = (LocationManager)mContext.getSystemService(context); -- } }
所以在你的activity类中,像这样在onCreate
函数中创buildfyl的对象:
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); } }
你可以去这个:
getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
我得到的一个方法是通过创build一个静态类的实例。 我在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);
我不知道这是否会有所帮助,但我做到了这一点:
LocationManager locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
(LocationManager) ((lmt) new Activity()).getSystemService(context);
试试这个代码。 这只会在调用这行代码之前启动活动时才起作用。 活动必须创build并运行。