2013-01-05 30 views
1

我的Android項目提供基於位置的提醒。在服務中,我計算從當前位置到數據庫中所有位置的距離。如果距離小於500米,則向用戶發出通知。問題在於,由於計算距離和發送通知是在服務內完成的,所以如果距離爲< 500m,通知對於相同位置不斷重複。最終,應用程序必須被強制關閉。下面是服務類的onLocationChanged方法:如何限制Android中服務發送的通知數量?

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

您必須添加一些邏輯到您的服務,以便更仔細地決定何時觸發通知。例如,您可以在表格中添加一列,用於跟蹤用戶當前是否「足夠接近」(在您的示例中爲< 500)到要通知的給定位置;每當您重新計算距離時,如果您發現現在「足夠接近」,但該列尚未反映出該距離,則您知道用戶剛剛進入該位置的附近,現在是排隊通知的時間關於它。在數據庫中記下用戶現在靠近該位置的位置,以便您可以抑制有關該位置的多餘通知。一旦離開鄰近區域,一定要清除「足夠靠近」位。

如果你對「足夠接近」的邊緣仍然有一些多餘的通知,所以你需要添加一些反彈,但這應該讓你在你的路上。

+0

非常感謝您的建議。這是一個非常好的主意,我一定會嘗試。如果這看起來是一個愚蠢的問題,我很抱歉,但是什麼是反彈?再次感謝一次。 :) – user1625971

+0

[debouncing](http://en.wiktionary.org/wiki/debounce)基本上是指確保你不會因意外而重複激活一堆。例如,您可能想要禁止雙擊購物車中的「購買」按鈕,因爲您不想向用戶收取兩次費用,或者(在這種情況下)確保當您處於邊緣位置時的一個位置,你沒有得到一堆in/out/in/out轉換。 – dsandler

相關問題