When alarm is set, the old data cannot be deleted when data is transmitted through intent.

Source: Internet
Author: User

When creating a project, you must dynamically create a scheduled task in the service. I have encountered several problems. Let's talk to you here. If something is wrong, please point out. Thank you.

1. When multiple timers are created, the old data will be retained by transmitting data through bundle.

For example, if you first define two tasks, and then re-open the software and then define two, then you will find that the value transmitted by the first task is the data of the first 2nd tasks.

2. The timer cannot be canceled after setaction is created when intent is created.

 

When dealing with the first problem above, I found a piece of information on the Internet and said that the problem needs to be solved under each setaction. Note the setaction line.

 1   /**  
2 * @ Param Alarm timing
3 * @ Param The value to be transferred by bundle is similar to that of map.
4 * @ Param Time task Start Time
5 * @ Param Code task no.
6 */
7 Private Void Createalarm (alarmmanager alarm, bundle, Long Time,Int Code ){
8 Intent intent = New Intent ( This , Playactivity. Class );
9 Intent. setaction (string. valueof (system. currenttimemillis ()));
10 Intent. putextra (constants. pm_data_key, bundle );
11 Pendingintent = pendingintent. getactivity ( This , Code, intent, 0 );
12 Alarm. Set (alarmmanager. rtc_wakeup, time, pendingintent );
13 }

However, the second problem is raised here. After finding the information, we find that the last parameter pendingintent. flag_update_current is modified when pendingintent is set, and setaction is commented out. It is strange that both problems are okay.

After modificationCodeNote:

 1   /**  
2 * @ Param Alarm timing
3 * @ Param The value to be transferred by bundle is similar to that of map.
4 * @ Param Time task Start Time
5 * @ Param Code task no.
6 */
7 Private Void Createalarm (alarmmanager alarm, bundle, Long Time,Int Code ){
8 Intent intent = New Intent ( This , Playactivity. Class );
9 // Intent. setaction (string. valueof (system. currenttimemillis ()));
10 Intent. putextra (constants. pm_data_key, bundle );
11 Pendingintent = pendingintent. getactivity (This , Code, intent, pendingintent. flag_update_current );
12 Alarm. Set (alarmmanager. rtc_wakeup, time, pendingintent );
13 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.