如何限制android中服务发送的通知数量?
Posted
技术标签:
【中文标题】如何限制android中服务发送的通知数量?【英文标题】:How to limit the number of notifications being sent by a service in android? 【发布时间】:2013-01-05 10:49:49 【问题描述】:我的 android 项目提供基于位置的提醒。在服务中,我正在计算从当前位置到数据库中所有位置的距离。如果距离小于 500m,则通知用户。问题是,由于距离的计算和通知的发送是在服务内完成的,如果距离
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();
当我靠近某个特定位置时,我怎样才能让一个通知只出现一次,而不是重复出现?任何帮助将不胜感激。非常感谢!
【问题讨论】:
【参考方案1】:您必须在服务中添加一些逻辑,以便更仔细地决定何时触发通知。
例如,您可以在表中添加一列,用于跟踪用户当前是否“足够接近”(在您的示例中为
如果您正处于“足够接近”的边缘,仍然会有一些多余的通知,因此您需要添加一些去抖动功能,但这应该能让您顺利上路。
【讨论】:
非常感谢您的建议。这是一个非常好的主意,我一定会尝试的。如果这似乎是一个愚蠢的问题,我很抱歉,但什么是去抖动?再次感谢一吨。 :) debouncing 基本上意味着确保您不会意外获得一堆重复激活。例如,您可能希望禁止意外双击购物车中的“购买”按钮,因为您不想向用户收取两次费用,或者(在这种情况下)确保当您处于边缘时的位置,您不会连续获得一堆输入/输出/输入/输出转换。以上是关于如何限制android中服务发送的通知数量?的主要内容,如果未能解决你的问题,请参考以下文章
Android BLE 中每个连接发送的数据包数量是不是有限制?