Android BroadcastReceiver实现LocationListener:如何在onLocationChanged中获取上下文
我的问题很简单:我有一个Android BroadcastReceiver实现LocationListener:如何在onLocationChanged中获取上下文,android,broadcastreceiver,android-context,locationlistener,Android,Broadcastreceiver,Android Context,Locationlistener,我的问题很简单:我有一个BroadcastReceiver,它实现了LocationListener。LocationListener需要通常未实现的方法,这些方法不在BroadcastReceiver的onReceive范围内,因此我没有上下文可供使用。问题是在onLocationChanged方法(其中一个LocationListener未实现的方法)中,我必须调用一个必须使用上下文的方法,我不知道如何获取它 public class MyBroadcastReceiver extends
BroadcastReceiver
,它实现了LocationListener
。LocationListener
需要通常未实现的方法,这些方法不在BroadcastReceiver
的onReceive
范围内,因此我没有上下文可供使用。问题是在onLocationChanged
方法(其中一个LocationListener
未实现的方法)中,我必须调用一个必须使用上下文的方法,我不知道如何获取它
public class MyBroadcastReceiver extends BroadcastReceiver implements LocationListener {
@Override
public void onReceive(Context context, Intent intent) {
}
public void onLocationChanged(Location location) {
method(context);
}
public void onStatusChanged(String s, int i, Bundle b) {
}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}
}
如何实现这一点?您可以像下面这样声明一个类级上下文,然后使用它 公共类MyBroadcastReceiver扩展BroadcastReceiver实现LocationListener {
您可以像下面这样声明一个类级上下文,然后使用它 公共类MyBroadcastReceiver扩展BroadcastReceiver实现LocationListener { 我有一个实现LocationListener的BroadcastReceiver 如果这个
BroadcastReceiver
是通过清单注册的,那么让它成为LocationListener
是没有意义的,因为您的进程可能在onReceive()
返回数毫秒后终止
我必须调用一个必须使用上下文的方法,但我不知道如何获取它
如果您正在使用registerReceiver()
设置此BroadcastReceiver
,并且出于某种原因,您也可以将其设置为LocationListener
,那么您的上下文
就是所谓的registerReceiver()
(活动
或服务
)
我有一个实现LocationListener的BroadcastReceiver
如果这个BroadcastReceiver
是通过清单注册的,那么让它成为LocationListener
是没有意义的,因为您的进程可能在onReceive()
返回数毫秒后终止
我必须调用一个必须使用上下文的方法,但我不知道如何获取它
如果您正在使用
registerReceiver()
设置此BroadcastReceiver
,并且出于某种原因,您也可以将其设置为LocationListener
,那么您的上下文
就是所谓的registerReceiver()
(活动
或服务
).没错。但是它是否真的使用了正确的上下文,或者它没有给我任何错误,因为我用“私有上下文”声明了变量,甚至可能是空的。我的意思是,自从onreceive之后,它只在广播被触发时才被调用,而不是在我从该类外部实例化该类时才被调用。你确定该方法正确吗(context)将获取在onReceive方法中传递的上下文,而不仅仅是通过声明“private context”获得的空上下文?这是真的。但是它是否真的使用了正确的上下文,或者它只是因为我使用“private context”声明了变量而没有给我错误这甚至可能是空的。我的意思是,由于onreceive只有在触发广播时才调用它,而不是在我从该类外部实例化该类时,您确定该方法(上下文)将在onreceive方法中传递上下文,而不仅仅是通过声明“private context context”获得的空上下文吗?我在这个broadcastreceiver中有很多方法,是的,我已经通过清单注册了它。当触发onReceive时,进程必须在它执行onReceive中的操作后立即终止。这很好。在其中一个方法中,我有RequestLocationUpdate,这就是为什么我需要LocationListener和上下文(我需要上下文,因为LocationManager LocationManager=(LocationManager)context.getSystemService(context.LOCATION_SERVICE);@user2911015:“当触发onReceive时,进程必须在执行onReceive内的操作后立即终止。这样就可以了”--那么调用requestLocationUpdates()就没有意义了
。我在这个broadcastreceiver中有很多方法,是的,我已经通过清单注册了它。当触发onReceive时,进程必须在它执行onReceive中的操作后立即终止。这很好。在其中一个方法中,我有RequestLocationUpdate,这就是为什么我需要LocationListener和上下文(我需要上下文,因为LocationManager LocationManager=(LocationManager)context.getSystemService(context.LOCATION_SERVICE);@user2911015:“当触发onReceive时,进程必须在执行onReceive内的操作后立即终止。这样就可以了”--那么调用requestLocationUpdates()
就没有意义了。
private Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.conext = context;
}
public void onLocationChanged(Location location) {
method(context); // Now you can use context
}
public void onStatusChanged(String s, int i, Bundle b) {
}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}