我正在编写一个家庭自动化程序。我有一个片段,当你与它交互时,它将切换远程设备。在收到设备的响应后,我更新片段中的图像视图以反映设备的状态(灯泡打开/灯泡关闭)。我也有一个小部件做同样的事情。
我正在使用AsyncTasks来处理远程调用。为了实现小部件和片段之间的一致性(例如,如果我使用小部件关闭灯光,片段中的图像也会更新)我正在拥有发送该切换请求寄存器本身的所有内容(在onUpdate中)小部件,以及片段的onCreateView)作为其他切换事件的监听器。我想这会让我有两个听众。片段和小部件。但是,在实践中,我看到了5个或6个小部件实例,我不知道为什么。这是我的听众代码。
public class LightToggleTask extends SparkAPITask {
private static String api_path = "toggle";
private static HashMap<OnTaskCompleted, Context> statusListeners = new HashMap<OnTaskCompleted, Context>();
public LightToggleTask(String deviceID,OnTaskCompleted listener, Context context) {
super(deviceID, api_path, listener, context);
statusListeners.put(listener, context);//adding listeners to the hashmap
}
public static void registerForToggleEvent(OnTaskCompleted listener, Context context){
statusListeners.put(listener, context);
}
@Override
public void onPostExecute(JSONArray jsonArray){
Set entries = statusListeners.entrySet();
Iterator i = entries.iterator();
while(i.hasNext()){
Map.Entry entry = (Map.Entry)i.next();
Log.i(TAG, "onPostExecute() -> Notifying observer that task has completed: " + entry.toString());
((OnTaskCompleted)entry.getKey()).onTaskCompleted(jsonArray, (Context)entry.getValue());
}
}
我在日志中获得的内容是这样的:
11-05 10:37:51.470 25437-25437/com.example.hcp.home_control_prototype I/SparkAPITask﹕ onPostExecute() -> Notifying observer that task has completed: com.example.hcp.home_control_prototype.WidgetProvider@4260a528=android.app.ReceiverRestrictedContext@426cfa68
11-05 10:37:51.490 25437-25437/com.example.hcp.home_control_prototype I/SparkAPITask﹕ onPostExecute() -> Notifying observer that task has completed: com.example.hcp.home_control_prototype.WidgetProvider@425de730=android.app.ReceiverRestrictedContext@426cfa68
11-05 10:37:51.490 25437-25437/com.example.hcp.home_control_prototype I/SparkAPITask﹕ onPostExecute() -> Notifying observer that task has completed: com.example.hcp.home_control_prototype.WidgetProvider@42623750=android.app.ReceiverRestrictedContext@426cfa68
11-05 10:37:51.500 25437-25437/com.example.hcp.home_control_prototype I/SparkAPITask﹕ onPostExecute() -> Notifying observer that task has completed: com.example.hcp.home_control_prototype.WidgetProvider@426cfa50=android.app.ReceiverRestrictedContext@426cfa68
11-05 10:37:51.500 25437-25437/com.example.hcp.home_control_prototype I/SparkAPITask﹕ onPostExecute() -> Notifying observer that task has completed: com.example.hcp.home_control_prototype.WidgetProvider@426305c8=android.app.ReceiverRestrictedContext@426cfa68
11-05 10:37:51.500 25437-25437/com.example.hcp.home_control_prototype I/SparkAPITask﹕ onPostExecute() -> Notifying observer that task has completed: com.example.hcp.home_control_prototype.WidgetProvider@42691b20=android.app.ReceiverRestrictedContext@426cfa68
11-05 10:37:51.500 25437-25437/com.example.hcp.home_control_prototype I/SparkAPITask﹕ onPostExecute() -> Notifying observer that task has completed: ToggleLightFragment{42640d38 #0 id=0x7f09005b}=null
我可能会做一些非常愚蠢的事情。我是Android开发的新手,我不确定我错过了什么。好像即使我只有一个小部件,当我将它注册为监听器时,它每次都返回一个不同的密钥?我有更好的方法来做这件事吗?