[英]How can I send result data from Broadcast Receiver to Activity
我有一个调用广播接收器的活动。 广播接收器等待并监听 GPS。 当侦听器获得新点时,我想将该新点发送给 Activity。 如何将数据从广播接收器发送到活动?
我的活动中需要一个监听器,等待广播接收器的响应。 我怎样才能做到这一点?
您可以从您的活动中呼叫接收者。 如果您不想在活动中添加接收器的逻辑,您可以使用抽象接收器。
你抽象接收者:
public abstract class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Add you receiver logic here
...
...
onNewPosition();
}
protected abstract void onNewPosition();
}
在您的活动中:
public class MyActivity extends Activity {
private smsReceiver smsReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_one_position);
smsReceiver = new smsReceiver() {
// this code is call asyncrously from the receiver
@Override
protected void onNewPosition() {
//Add your activty logic here
}
};
IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
intentFilter.setPriority(999);
this.registerReceiver(smsReceiver, intentFilter);
}
@Override
protected void onPause() {
super.onPause();
this.unregisterReceiver(this.smsReceiver);
}
}
我希望它会帮助你...
我为我的接收器定义了一个监听器并在活动中使用它,它现在运行完美。 以后有可能出现问题吗?
public interface OnNewLocationListener {
public abstract void onNewLocationReceived(Location location);
}
在我的接收器 class 中,它被命名为 ReceiverPositioningAlarm:
// listener ----------------------------------------------------
static ArrayList<OnNewLocationListener> arrOnNewLocationListener =
new ArrayList<OnNewLocationListener>();
// Allows the user to set an Listener and react to the event
public static void setOnNewLocationListener(
OnNewLocationListener listener) {
arrOnNewLocationListener.add(listener);
}
public static void clearOnNewLocationListener(
OnNewLocationListener listener) {
arrOnNewLocationListener.remove(listener);
}
// This function is called after the new point received
private static void OnNewLocationReceived(Location location) {
// Check if the Listener was set, otherwise we'll get an Exception when
// we try to call it
if (arrOnNewLocationListener != null) {
// Only trigger the event, when we have any listener
for (int i = arrOnNewLocationListener.size() - 1; i >= 0; i--) {
arrOnNewLocationListener.get(i).onNewLocationReceived(
location);
}
}
}
在我的活动方法之一中:
OnNewLocationListener onNewLocationListener = new OnNewLocationListener() {
@Override
public void onNewLocationReceived(Location location) {
// do something
// then stop listening
ReceiverPositioningAlarm.clearOnNewLocationListener(this);
}
};
// start listening for new location
ReceiverPositioningAlarm.setOnNewLocationListener(
onNewLocationListener);
你有几种方法可以做到这一点和几个考虑因素。
您可以轮询,这意味着使用 Handler 或 Timer 每隔一段时间再次检查一次以查看信息是否已到达。
您可以将广播接收器注册为活动的内部 class ,然后您可以在活动中调用方法。
关于一些考虑,BroadCastReciver 主要用作侦听器,而不是内部 class 的通知器,在我看来,与活动一起使用是最佳实践,对于服务,您可以将其用作独立的 class 并在 Manifest.ZA2F6735D0E0F32874 中注册。 .. 现在您必须记住,当广播正在广播时,您的 Activity 可能由于方向更改或暂停您的应用程序的事件而处于非活动状态,因此您可能会错过该事件。 我不听系统事件,而是听我自己的事件,所以我使用粘性广播来防止这个问题。
只需要在activity中实现广播接收器。 使用活动的上下文注册接收者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.