Android服务访问另一个类的静态成员
我的android服务有问题。在“onLocationChanged”中,我想访问类“Device”的静态成员 我以以下方式开始我的服务:Android服务访问另一个类的静态成员,android,service,static,locationlistener,Android,Service,Static,Locationlistener,我的android服务有问题。在“onLocationChanged”中,我想访问类“Device”的静态成员 我以以下方式开始我的服务: Intent i = new Intent(this, LocationService.class); startService(i); 这是我的类LocationListener和服务: private class LocationListener implements android.location.LocationListener{
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);
}
}
和类LocationType
public class LocationType {
private Location location;
public LocationType(){
//...
}
Location getLocationData() {
return location;
}
void setLocationData(Location location) {
this.location = location;
}
}
我的应用程序失败,因为我无权从服务访问静态成员?如何访问此静态成员
非常感谢您的回答。您的领域是私人的
private static LocationType locationType;
把它公之于众,它应该是可访问的
public static LocationType locationType;
如果不希望这样,您至少应该创建一个静态getter,它可以是公共的,也可以是包私有的。如果它是包私有的,请确保两个类都在同一个包中。您的字段是私有的
private static LocationType locationType;
把它公之于众,它应该是可访问的
public static LocationType locationType;
如果不希望这样,您至少应该创建一个静态getter,它可以是公共的,也可以是包私有的。如果它是包私有的,请确保两个类都在同一个包中。您使用的是
Device.getLocationType()
您确定此方法也是静态的吗?因此,要么将其设置为静态,要么将locationType
设置为公共,并直接访问它
你是否愿意完全是另一回事
此外,尝试替换:
private static LocationType locationType;
与:
private static LocationType locationType = new LocationType();
您使用的是
Device.getLocationType()
您确定此方法也是静态的吗?因此,要么将其设置为静态,要么将locationType
设置为公共,并直接访问它
你是否愿意完全是另一回事
此外,尝试替换:
private static LocationType locationType;
与:
private static LocationType locationType = new LocationType();
我的对象“locationType”始终定义为空。。。在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);
}
}
我的服务已启动,但在服务方法“onLocationChanged”中,Device.locationType为null 我的对象“locationType”总是定义为空。。。在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);
}
}
我的服务已启动,但在服务方法“onLocationChanged”中,Device.locationType为null 是的,设备中的getLocationType()方法是静态的。并返回LocationType的对象。我想调用这个对象的方法“setLocationData”。(“setLocationdData”受“保护”,因此可以在同一个包中访问。)这很奇怪,但在控制台日志中,没有显示任何内容。只有应用程序显示消息“应用程序已停止”。我单击“确定”,应用程序仍在运行。每次程序进入“onLocationChanged”时都会显示此消息。当我调用对象“LocationType”的方法时,服务将停止。我已尝试仅检索对象,但它仍然有效:LocationType=Device.getLocationType();当我访问此对象的非静态方法或成员时,服务将停止。如何从“onLocationChanged”访问此对象的成员和方法?是的,谢谢您的评论。我的对象位置类型为空。。。。现在,我来看看为什么它是空的。。谢谢您的帮助。是的,设备中的getLocationType()方法是静态的。并返回LocationType的对象。我想调用这个对象的方法“setLocationData”。(“setLocationdData”受“保护”,因此可以在同一个包中访问。)这很奇怪,但在控制台日志中,没有显示任何内容。只有应用程序显示消息“应用程序已停止”。我单击“确定”,应用程序仍在运行。每次程序进入“onLocationChanged”时都会显示此消息。当我调用对象“LocationType”的方法时,服务将停止。我已尝试仅检索对象,但它仍然有效:LocationType=Device.getLocationType();当我访问此对象的非静态方法或成员时,服务将停止。如何从“onLocationChanged”访问此对象的成员和方法?是的,谢谢您的评论。我的对象位置类型为空。。。。现在,我来看看为什么它是空的。。谢谢您的帮助。我有一个用于此私有静态成员的getter:public static LocationType getLocationType(){return Device.LocationType;},我使用该getter在OnLocationChanged LocationType=Device.getLocationType()方法中获取对象LocationType;尽量不要像
Device.locationType
那样引用它,就像locationType
一样购买。由于该方法已经是静态的,您应该能够像那样访问它。已经尝试过了。我读到:我想我的情况也是如此。但是我不明白如何更正它……我有一个用于这个私有静态成员的getter:public static LocationType getLocationType(){return Device.LocationType;},我使用getter在OnLocationChanged LocationType=Device.getLocationType()方法中获取对象LocationType;尽量不要像Device.locationType
那样引用它,就像locationType
一样购买。由于该方法已经是静态的,您应该能够像那样访问它。已经尝试过了。我读到:我想我的情况也是如此。但我不明白我怎么能纠正它。。。。