开发者

Android Pending intent?

I have pending intent that sets alarm and it takes parameters such as row id and time from a database. I want to cancel alarm so i would do that by sending another pending intent with the same info and then cancel(i want to cancel it from different file). I only allow one alarm to be set at anyone time because that's the way my app works, because there is only one alarm set from that pendin开发者_如何学运维g intent is there anyway i can just do cancel all for that intent?


Since I believe in code samples to get the point across, see below:

/*
 * An alarm can invoke a broadcast request
 * starting at a specified time and at 
 * regular intervals.
 */
public void sendRepeatingAlarm()
{
    Calendar cal = Utils.getTimeAfterInSecs(30);

    String s = Utils.getDateTimeString(cal);
    this.mReportTo.reportBack(tag, "Schdeduling Repeating alarm in 5 sec interval starting at: " + s);

    //Get an intent to invoke TestReceiver class
    Intent intent = new Intent(this, TestReceiver.class);
    intent.putExtra("message", "Repeating Alarm");

    PendingIntent pi = this.getDistinctPendingIntent(intent, 2);

    // Schedule the alarm!
    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

    am.setRepeating(AlarmManager.RTC_WAKEUP, 
            cal.getTimeInMillis(), 
            5*1000, //5 secs 
            pi);
}

protected PendingIntent getDistinctPendingIntent(Intent intent, int requestId)
{
    PendingIntent pi = 
        PendingIntent.getBroadcast(
          this,         //context
          requestId,    //request id
          intent,       //intent to be delivered
          0);

    //pending intent flags
    //PendingIntent.FLAG_ONE_SHOT);     
    return pi;
}

/*
 * An alarm can be stopped by canceling the intent.
 * You will need to have a copy of the intent
 * to cancel it.
 * 
 * The intent needs to have the same signature
 * and request id.
 */
public void cancelRepeatingAlarm()
{
    //Get an intent to invoke TestReceiver class
    Intent intent = new Intent(this, TestReceiver.class);

    //To cancel, extra is not necessary to be filled in
    //intent.putExtra("message", "Repeating Alarm");

    PendingIntent pi = this.getDistinctPendingIntent(intent, 2);

    // Schedule the alarm!
    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    am.cancel(pi);
}


I have pending intent that sets alarm and it takes parameters such as row id and time from a database.

There are no "parameters" on a PendingIntent. I am going to interpret this as meaning "extras".

I want to cancel alarm so i would do that by sending another pending intent with the same info and then cancel(i want to cancel it from different file).

It's not "sending" but "creating". Otherwise, yes, this is correct.

i can just do cancel all for that intent?

There is only one alarm.

Extras do not matter in terms of alarm scheduling. If you have an Intent (I1) wrapped in a PendingIntent (PI1) and use that to schedule an alarm, and later if you create an Intent (I2) with the same component/action/data/type, wrap that in a PendingIntent (PI2) and cancel() the alarm, it will cancel the PI1 alarm. Similarly, if you use PI2 to schedule a new alarm, it will remove the old PI1 alarm.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜