0

我在这里看到了 Commonwares LocationPoller 演示 github.com/commonsguy/cwac-locpoll/

在这里,LocationReceiver我正在检查该位置是否在指定区域内。如果不在那个区域,我会向电话号码发送短信(SMS)。

但它会在每个特定间隔接收新位置时连续发送文本消息(SMS)。所以我定义了一个类变量flag=0(最初)

            public class LocationReceiver extends BroadcastReceiver {
                int flag=0;
            public void onReceive(Context context, Intent intent) {

            ............


            if( flag==0)    //center of campus 
                      {
                          checkArea(loc,"500","22.599669","72.820473","5556","Your ward is out of College campus");
                          Toast.makeText(context, "You Have Moved out",Toast.LENGTH_SHORT);
                          flag=1;
                      }
                }
            }

但它不起作用,它仍然连续发送短信。我应该怎么办 ?

4

2 回答 2

2

使用 SharedPreferences 发送消息时保存一个 unix 时间戳,以便下次触发广播时检查所述时间戳的存在。如果时间戳不存在或它足够旧(实际时间 - 保存的时间戳 >= 您定义的某些阈值),您只会发送消息,在发送消息时更新或创建时间戳。

于 2013-04-18T19:18:43.317 回答
1

您可以使用持久性存储来存储状态,并且每次收到通知时都会检查您的存储是否必须发送 SMS。

于 2013-04-18T19:14:47.300 回答