多个警报呼叫相同的服务

时间:2015-09-15 12:01:30

标签: android service location alarmmanager

我有多个叫做广播意图的闹钟,后者又调用了一个位置服务。我们的想法是在不同的时间获取位置。位置服务需要一些时间才能返回位置,然后关闭。 我想知道在2个警报同时调用此公共位置服务或者说警报1已经称为位置服务并且在第二个警报2中也称为位置服务的情况下会发生什么,因为我已经读取服务只有1个实例。 / p>

还有最好的方法来处理像我上面提到的那样的情况吗?

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中,我调用服务

Intent serviceIntent = new Intent(context,MyLocationService.class);
    context.startService(serviceIntent); //start service for get location

可以通过此未决意图调用同一服务的某些部分之间安排多个警报。

2 个答案:

答案 0 :(得分:0)

是的,你没错。我显示了我的代码,我的请求代码是i变量。你可以为你改变它

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++;
        }
    }

答案 1 :(得分:0)

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

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*/}
}

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