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 }