#1 每当位置发生变化时,发送意图并在您想要的地方接收它
LocalBroadcastManager mLocalBroadcastManager;
@Override
public void onLocationChanged(Location location) {
Intent intent = new Intent();
intent.putExtra("com.exmaple.sample", location);
mLocalBroadcastManager.sendBroadcast(intent);
}
#2 每当位置改变时,发送意图并在你想要的地方接收它(与 1 相同)
@Override
public void onLocationChanged(Location location) {
Intent intent = new Intent();
intent.putExtra("com.exmaple.sample", location);
sendBroadcast(intent);
}
#3 或者,只传递一次未决意图,然后接收位置
Intent intent = new Intent(mContext, YourBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 1, intent, 0);
mLocManager = (LocationManager)getSystemService(LOCATION_SERVICE);
mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, pendingIntent);
#4 创建一个静态变量,然后定期从活动中轮询它。请注意,您应该优化轮询频率,因为不能保证获得新的位置。这可能是不好的方法,但对于您的场景来说可能是值得的
public static Location sLastLocation;
@Override
public void onLocationChanged(Location location) {
sLastLocation = location;
}
#5 使用事件总线,link