我的問題很簡單:我有一個BroadcastReceiver
實現LocationListener
。 LocationListener
需要通常未實現的方法,那些在BroadcastReceiver
的onReceive
之外,所以我沒有使用的上下文。事情是在onLocationChanged
方法(其中一個LocationListener
未實現的)之一,我必須調用一個方法必須使用上下文,我不知道如何得到它。BroadcastReceiver實現LocationListener:如何獲取onLocationChanged中的上下文
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) {
}
}
我該如何做到這一點?
的確如此。但是它真的使用了正確的上下文嗎?它只是給了我沒有錯誤,因爲我已經用「私有上下文上下文」聲明瞭該變量,甚至可能是空的。我的意思是,自從接受它只在廣播被觸發時才被調用,而不是在我從該類外部實例化類的時候,你確定方法(上下文)將獲得在onReceive方法中傳遞的上下文,而不是僅僅獲得一個空方法通過聲明「私人上下文」? – user2911015