如何限制通知的数量在Android的服务发送? [英] How to limit the number of notifications being sent by a service in android?

查看:210
本文介绍了如何限制通知的数量在Android的服务发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid项目为基于位置的reminders.In服务,我要计算数据库中的所有的位置从当前位置的距离。如果距离不到500米,一个通知被提供给用户。的问题是,由于距离的计算和通知发送正在服务内完成,通知使多次来为如果该距离相同的位置与所述;500米。最终,应用程序已被强制关闭。下面是服务类的onLocationChanged方法:

My Android project gives location-based reminders.In a service,I am calculating the distance from the current location to all the locations in the database. If the distance is less than 500m,a notification is given to the user. The problem is that, since the calculation of distance and sending of notifications is being done within the service, the notifications keep coming repeatedly for the same location if the distance is<500m. Ultimately the app has to be force closed. Here is the onLocationChanged method of the service class:

    public void onLocationChanged(Location location) {
    double lat = location.getLatitude(),lng = location.getLongitude(),dblat = 0,dblng=0;

    Location currentlocation = new Location("current Location");
    currentlocation.setLatitude(lat);
    currentlocation.setLongitude(lng);

    int NOTIFICATION_ID = 1;
    String ns = Context.NOTIFICATION_SERVICE;

     String title,date,today;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        Calendar cal=Calendar.getInstance();
        today = String.valueOf(cal.get(Calendar.YEAR)) + "-"
                + String.valueOf(cal.get(Calendar.MONTH)) + "-"
                + String.valueOf(cal.get(Calendar.DAY_OF_MONTH));

    SQLiteDatabase database = new SQLiteHelper(getApplicationContext()).getWritableDatabase();
    Cursor c = database.rawQuery("select title,reminderdate,latitude,longitude from Reminder where type=1", null);


    int icon = R.drawable.ic_launcher;
    if(c.getCount()>0)
    { //System.out.println("c not null");
    c.moveToFirst();
    while(!c.isAfterLast())
    {
        title=c.getString(0);
        date=c.getString(1);
        if(date.equals(today))
        {
        dblat=c.getDouble(2);
        dblng=c.getDouble(3);
        Location dblocation = new Location("db Location");
        dblocation.setLatitude(dblat);
        dblocation.setLongitude(dblng);

        Double distance = (double) currentlocation.distanceTo(dblocation);
    if(distance<500)

        //Toast.makeText(this, "distance " + distance, Toast.LENGTH_LONG).show();

      {    long when = System.currentTimeMillis();
          Notification notification = new Notification(icon,title, when);

          RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
          contentView.setImageViewResource(R.id.notification_image, R.drawable.ic_launcher);
          contentView.setTextViewText(R.id.notification_title, "My custom notification title");
          contentView.setTextViewText(R.id.notification_text, "My custom notification text");
          notification.contentView = contentView;

          Intent notificationIntent = new Intent(this, ShowReminder.class);
          notificationIntent.putExtra("title", title);
          PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

          notification.contentIntent = contentIntent;

          notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
          notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
          notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
          notification.defaults |= Notification.DEFAULT_SOUND; // Sound

          mNotificationManager.notify(NOTIFICATION_ID, notification);       
    }   }
        c.moveToNext();
    }}

    c.close();
    database.close();

}

如何让只有一个通知一个特定的位置来只有一次,只要我靠近它,而不是它的到来反复?任何帮助将大大AP preciated。非常感谢!

How can I make only a single notification come only once for a particular location whenever I am near it instead of it coming repeatedly? Any help will be greatly appreciated. Thanks a lot!

推荐答案

您将有一些逻辑添加到您的服务更仔细的通知时,应该触发决定。

You'll have to add some logic to your service to decide more carefully when notifications should be triggered.

例如,您可以添加一列到表,跟踪用户是否正在足够接近(小于你的榜样500),以被通知给定位置;每次重新计算距离,如果你发现你现在是足够接近,但列还没有反映这一点,你知道用户才刚刚进入该位置附近,它的时间来排队的通知关于它。使用户目前已接近该位置,这样就可以想喝该地点preSS冗余通知的DB的说明。一定要清楚,一旦你离开了附近的足够接近位。

For example, you might add a column to your table that tracks whether the user is currently "close enough" (<500 in your example) to a given location to be notified; each time you re-compute distances, if you discover that you are now "close enough" but the column doesn't yet reflect that, you know that the user has only just entered the proximity of that location and it's time to enqueue a notification about it. Make a note in the DB that the user is now close to that location, so that you can suppress redundant notifications about that location. Be sure to clear the "close enough" bit once you leave the proximity.

仍然会有一些多余的通知,如果你是对的就足够接近,所以你需要添加一些反跳边,但这应该让你对你的方式。

There will still be some redundant notifications if you're right on the edge of "close enough" so you'll need to add some debouncing, but this should get you on your way.

这篇关于如何限制通知的数量在Android的服务发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆