在Android中给定时间后取消通知

时间:2014-06-23 05:08:25

标签: android notifications

我正在尝试使用AlarmManager在给定时间后取消通知。

比如说我每天上午9点通过AlarmManger设置通知。

我想在每天上午9:30通过AlarmManager自动取消相同的通知。

因此,我尝试了以下内容:

以下代码在Notification Service

中设置
public class ServiceForNotification extends Service 
{
private String nText;
private Resources res;

@Override
public void onCreate() 
{
    super.onCreate();

    nText = "TESTING NOTIFICATION";


        Context context = ServiceForNotification.this;
        Intent notificationIntent = new Intent(this, ServiceToCancelNotification.class);
        PendingIntent contentIntent = PendingIntent.getService(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        RemoteViews bigView = new RemoteViews(context.getPackageName(), R.layout.notificationlayoutbig);
        bigView.setTextViewText(R.id.noticeText, nText);


        Notification noti = new Notification.Builder(this)
        .setContentTitle(nText)
        .setPriority(Notification.PRIORITY_MAX)
        . setContentText ("Slide down to Expand"). setSmallIcon(R.drawable.close)
        .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.close))
        .setTicker("TEST")
        .setAutoCancel(true)
        .setWhen(System.currentTimeMillis())
        .setContentIntent(contentIntent)
        .setLights(Color.MAGENTA, 500, 500).build();
        noti.flags |= Notification.FLAG_ONGOING_EVENT | Notification.PRIORITY_MAX;
        noti.bigContentView = bigView;
        notificationManager.notify(100, noti);

        /*18000000*/
        Intent notificationIntent = new Intent(this, ServiceToCancelNotification.class);
        PendingIntent contentIntent = PendingIntent.getService(ServiceForNotification.this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am2 = (AlarmManager) getSystemService(ALARM_SERVICE);
        am2.setExact(AlarmManager.RTC_WAKEUP, 90000, contentIntent); 


    Log.e("Alarm Set","TO CANCEL");

    stopSelf();
}

}

我有另一项名为ServiceToCancelNotification的服务..

其中包含以下代码:

public class ServiceToCancelNotification extends Service   
{ 

@Override
public void onCreate() 
{
    super.onCreate();

    Log.e("Alarm","in CANCEL");

    //Cancel Notification   
    NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    int unique_id = 100;
    nm.cancel(unique_id);

    Log.e("Alarm","Notifiy CANCEL");

    stopSelf();
  }

}

触发警报管理器后,不会调用此ServiceToCancelNotification。我不确定为什么会这样?

有人可以帮我解决这个问题吗?

谢谢!

1 个答案:

答案 0 :(得分:-1)

// Alarm manager and pending intent should be global variables, and/or the instance should be passed to the cancelAlarm
AlarmManager am= null;
PendingIntent pi = null;
@Override
    public void onReceive(Context context, Intent intent) 
    {
            am=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
        pi = PendingIntent.getBroadcast(context, ALARM_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}


public void CancelAlarm()
    {
        am.cancel(pi);
    }

public void CancelAlarm(AlarmManager am, PendingIntent pi)
    {
        am.cancel(pi);
    }