首先,我知道这个问题已经被问了很多,唯一的答案是为经理和未决的意图设置一个唯一的ID,所以我做了:
这是代码:
@Override
public int onStartCommand(Intent i , int flags , int startId){
try{
//UnqueID = i.getExtras().getInt(PassedUNIQUEID);
UniqueID++;
Log.d("SERVICECHECK" , UniqueID + "");
mStopIntent = new Intent(this , Service_reminder.class);
sTimeUntilNotification = 0;
CalculateTimeInMilSeconds(i);
final String Title = i.getExtras().getString(PassedSharedPref);
final String Note = i.getExtras().getString(PassedName);
//
Intent notificationIntent = new Intent(Service_reminder.this, activity_todialog_notecontent.class);
notificationIntent.putExtra(activity_todialog_notecontent.EXTRA_SUBJECTNAME, Title);
notificationIntent.putExtra(activity_todialog_notecontent.EXTRA_CONTENT, Note);
PendingIntent contentIntent = PendingIntent.getActivity(Service_reminder.this, UniqueID , notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.iconsubjecttracker)
.setContentTitle(Title)
.setContentText(Note);
builder.setContentIntent(contentIntent);
builder.setAutoCancel(true);
Uri AlarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(AlarmSound);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Log.d("SERVICECHECK" , sTimeUntilNotification + "");
CountDownTimer Timer = new CountDownTimer(sTimeUntilNotification , 1000) {
@Override
public void onTick(long millisUntilFinished) {
//manager.notify(0, builder.build());
}
@Override
public void onFinish() {
manager.notify(UniqueID, builder.build());
stopService(mStopIntent);
}
}.start();
}catch(NullPointerException e){
//handle
}
return START_STICKY;
}
它在时间上没有问题,但问题是例如我设置提醒"稍后发出通知"在一个笔记上,例如我放了2分钟,然后我转到下一个注释,并提出一个提醒,然后说一分钟,然后第一个启动没有问题,第二个启动,然后更新第一个?因此,用户将看到最后一次触发的通知,我不知道如何解决这个问题。
编辑:
好吧,我已经迷失了,我已经尝试解决它几个小时没有任何工作,从声明一个静态变量并每次调用服务时添加1,在共享的prefrences中加载一个唯一的id并加载然后更新它,但没有任何效果。我将代码编辑为我所做的最新事情,它再次添加一个静态变量并每次添加一个,因为它是我认为最干净的解决方案。
希望我明天能找到解决方案。
对于那些不想阅读所有内容的人来说,这是一个例子+我做过的照片:
首先是图片:
http://oi62.tinypic.com/2mrs9s0.jpg
正如你所看到的那样,我打电话给启动服务两次,1分钟提醒,另一个提醒2分钟,1和2是唯一ID,所以它不能使用相同的id。
问题的一个例子:
我有一张说明"数学"例如,我在2分钟后发出提醒,然后另一个说明"微积分"并且我在1分钟后发出提醒,1分钟后微积分通知没有问题,2分钟后只有声音弹出而不是数学通知,并且它保持不变,好像我只放了一个微积分音符。
编辑2:
我在使用闹钟管理器而不是倒计时之后解决了它。
答案 0 :(得分:0)
我在服务类
中创建的方法此处 SIMPLE_NOTFICATION_ID = 1
我也尝试过多个不同的 SIMPLE_NOTFICATION_ID ,但它不会删除之前的通知。意味着它运作良好。
public void getLocation1(){
timer = new Timer();
locationManager.
requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 0, this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, this);
handler = new Handler();
locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
mUpdateResults = new Runnable() {
public void run() {
getLatLongFromDatabase();
if(isGPSEnabled)
locationlist();
for(int ii=0;ii<Table_Values_length ; ii++)
{
if((((Dlat[ii]-0.0002) <= lat) && (lat <= (Dlat[ii]+0.0002))) && (((Dlng[ii]-0.0002) <= lng) && (lng <= (Dlng[ii]+0.0002))) )
{
String Tooast = name[ii];
int idtosent = ids[ii];
String a = String.valueOf(lat);
String b = String.valueOf(lng);
String c = String.valueOf(idtosent);
// Toast.makeText(getApplicationContext(),Tooast+"-ID="+idtosent, Toast.LENGTH_SHORT).show();
StopContinuousRun = true;
Intent notifyIntent = new Intent(ServiceHandler.this, OnNotificationClick.class);
notifyIntent.putExtra("data1", a);
notifyIntent.putExtra("data2", b);
notifyIntent.putExtra("data3", c);
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final PendingIntent Intent = PendingIntent.getActivity(getApplicationContext(), SIMPLE_NOTFICATION_ID, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notifyDetails = new Notification.Builder(ServiceHandler.this)
.setContentTitle("You Are at "+Tooast)
.setContentText("Click to View This Location Tasks")
.setSmallIcon(R.drawable.gps_icon)
.setContentIntent(Intent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.build();
notifyDetails.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
// final Notification notifyDetails = new Notification(R.drawable.ic_launcher,
// "You've got a new notification!",System.currentTimeMillis());
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
;
}
else
{
// Toast.makeText(getApplicationContext(),"Lat: "+lat +"\n Long: "+lng, Toast.LENGTH_SHORT).show();
StopContinuousRun = false;
}
}
}
};
int delay = 1000 * 5;
int period = 8000;
timer.scheduleAtFixedRate(new TimerTask() {
public void run(){handler.post(mUpdateResults); }}, delay, period);
}