我的Android服务有问题。在“onLocationChanged”中,我想访问我的类“Device”的静态成员。
我用以下方式开始服务:
Intent i = new Intent(this, LocationService.class);
startService(i);
这是我的类LocationListener,其中包含服务:
private class LocationListener implements android.location.LocationListener{
Location mLastLocation;
LocationListener(String provider)
{
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location)
{
Device.getLocationType().setLocationData(location);
}
@Override
public void onProviderDisabled(String provider)
{
//...
}
@Override
public void onProviderEnabled(String provider)
{
//...
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
//...
}
}
LocationListener[] mLocationListeners = new LocationListener[] {
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onStartCommand");
return START_STICKY;
}
@Override
public void onCreate()
{
super.onCreate();
Log.d(TAG, "onCreate");
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE,
mLocationListeners[1]);
Log.d(TAG, "try");
} catch (java.lang.SecurityException ex) {
Log.d(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE,
mLocationListeners[0]);
Log.d(TAG, "try2");
} catch (java.lang.SecurityException ex) {
Log.d(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy()
{
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
我的班级设备
public class Device {
private static LocationType locationType;
//...
}
类LocationType
public class LocationType {
private Location location;
public LocationType(){
//...
}
Location getLocationData() {
return location;
}
void setLocationData(Location location) {
this.location = location;
}
}
我的申请失败了,因为我无权从服务中访问静态成员?我如何访问这个静态成员?
非常感谢您的回答。
答案 0 :(得分:0)
你的字段是私人的
private static LocationType locationType;
公开并且应该可以访问
public static LocationType locationType;
如果不是这样,你至少应该制作一个静态的getter,它是public或package private。如果它的包是私有的,请确保这两个类都在同一个包中。
答案 1 :(得分:0)
你使用Device.getLocationType()
你确定这个方法也是静态的吗?因此,要么将其设为静态,要么公开locationType
并直接访问它。
你是否想要完全是另一回事。
另外,请尝试替换:
private static LocationType locationType;
with:
private static LocationType locationType = new LocationType();
答案 2 :(得分:0)
我的对象&#34; locationType&#34;始终定义为null ...在我的MainActivity中,我调用方法Device.init(...)。此方法在静态成员中创建对象LocationType。
public class Device {
private static LocationType locationType;
public static void init(...){
Device.locationType = new LocationType(...);
if(Device.locationType != null){
Intent i = new Intent(activity, LocationService.class);
activity.startService(i);
}
}
我的服务已启动,但在服务方法&#34; onLocationChanged&#34;中,Device.locationType为null。