首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个告警调用同一服务

多个告警调用同一服务
EN

Stack Overflow用户
提问于 2015-09-15 20:01:30
回答 2查看 315关注 0票数 0

我有多个报警调用广播意图,而广播意图又调用位置服务。这个想法是在不同的时间获得位置。位置服务需要一段时间才能返回位置,之后它会关闭。我想知道当两个警报同时调用这个公共位置服务时会发生什么,或者说警报1已经调用了位置服务,而第二个警报2也称为位置服务,因为我已经读取了服务只有一个实例。

还有,处理我上面提到的这种情况的最好方法是什么?

代码语言:javascript
复制
Intent intent = new Intent(context, NotificationView.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    alarmManager.set(AlarmManager.RTC_WAKEUP, dateTime.getTimeInMillis(), pendingIntent);

在我的NotificationView.class中,我调用服务

代码语言:javascript
复制
Intent serviceIntent = new Intent(context,MyLocationService.class);
    context.startService(serviceIntent); //start service for get location

可以在通过该待定意图调用该相同服务的某些secods的差异内调度多个警报。

EN

回答 2

Stack Overflow用户

发布于 2015-09-15 20:49:57

是的,你说得对。我展示了我的代码,其中我的请求代码是i变量。您可以为您更改它

代码语言:javascript
复制
public static void setAlarmReceiver(Context context, ArrayList<CheckPoint> checkPoints) {

        clearAlarmReceiver(context);

        if (checkPoints.size() == 0)
            return;

        int i = 0;
        for (CheckPoint checkPoint : checkPoints) {
            Intent intent = new Intent(context, CheckAlarmReceiver.class);
            intent.putExtra("checkPoint", checkPoint);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, Integer.valueOf(getDay(checkPoint.getDate())));
            calendar.set(Calendar.MONTH, Integer.valueOf(getMonth(checkPoint.getDate())) - 1);
            calendar.set(Calendar.YEAR, Integer.valueOf(getYear(checkPoint.getDate())));
            calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(getHour(checkPoint.getCheckInTime())));
            calendar.set(Calendar.MINUTE, Integer.valueOf(getMinute(checkPoint.getCheckInTime())));
            calendar.set(Calendar.SECOND, 0);
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            if (android.os.Build.VERSION.SDK_INT >= 19) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            } else {
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            }

            i++;
        }
    }
票数 0
EN

Stack Overflow用户

发布于 2015-09-15 20:52:10

在我看来,最好解决方案是创建辅助类,它将是缓存位置,并防止例如对服务的额外调用。

代码语言:javascript
复制
public class LocationHelper {
  public static AbstractMap.SimpleEntry<Long, Location> cache = null; //location stored here
  public static int expiryTime = 5000; // 5 seconds for example

  private static boolean requested = false;

  private static List<LocationListener> callbacks = new ArrayList<>();// request heap

  public static void getLocation(LocationManager lm, LocationListener ll){
    Long currentTimeStamp = getTimeStamp();
    final LocationListener mLocationListener = new LocationListener() {
       @Override
       public void onLocationChanged(final Location location) {
         cache = new AbstractMap.SimpleEntry<>(getTimeStamp(), location);

         synchronized(LocationHelper.class){
           for(LocationListener loclist : callbacks){
             loclist.onLocationChanged(location); // call each listener
           }
           callbacks.clear(); 
           requested = false;
         }
       }
    };
    if (cache != null && currentTimeStamp - cache.getKey() < expiryTime){
      ll.onLocationChanged(cache.getValue());
    } else {
       if(!requested){
         synchronized(LocationHelper.class){ // synchronize class to prevent thread collision and additional request
           if(!requested){
             lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, mLocationListener);
             requested = true;
           }
         }
       } else {
         synchronized(LocationHelper.class){
           callbacks.add(ll);
         }
       }
    }
  }

  public static Long getTimeStamp(){/*some code here*/}
}

这个解决方案有点复杂,但它应该是可行的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32585570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档