【发布时间】:2011-07-12 09:06:31
【问题描述】:
我有一个调用广播接收器的活动。广播接收器等待并监听 GPS。当侦听器获得新点时,我想将该新点发送给 Activity。如何将数据从广播接收器发送到活动?
我的活动中需要一个监听器来等待广播接收器的响应。我该怎么做?
【问题讨论】:
标签: android android-activity broadcastreceiver response
我有一个调用广播接收器的活动。广播接收器等待并监听 GPS。当侦听器获得新点时,我想将该新点发送给 Activity。如何将数据从广播接收器发送到活动?
我的活动中需要一个监听器来等待广播接收器的响应。我该怎么做?
【问题讨论】:
标签: android android-activity broadcastreceiver response
您可以从您的活动中呼叫接收方。如果您不想在活动中添加接收器的逻辑,您可以使用抽象接收器。
你抽象的接收者:
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);
}
在我的接收器类中被命名为 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 每隔一段时间再次检查一次 或计时器以查看信息是否已到达。
您可以将广播接收器注册为您的活动的内部类,然后您可以在您的活动中调用方法。
考虑到一些考虑,BroadCastReciver 主要用作侦听器,而不是 notider 内部类,在我看来,与活动一起使用是最佳实践,对于服务,您可以将其用作独立类并在清单中注册.xml... 现在您必须记住,当广播正在广播时,您的 Activity 可能由于方向更改或暂停您的应用程序的事件而处于非活动状态,因此您可能会错过该事件。我不听系统事件,而是听我自己的事件,所以我使用粘性广播来防止这个问题。
【讨论】:
只需要在activity中实现广播接收器即可。使用活动的上下文注册接收者。
【讨论】: