Android uses a timer to update the UI in onreciver.

Source: Internet
Author: User
Android uses a timer to update the UI in onreciver. In the timer thread, the UI cannot be directly updated. Handle must be used to register a broadcast, send a broadcast handler at the scheduled time and when the conditions are met. After receiving the broadcast, the handler updates the UI operations, here we also demonstrate how to transmit the context variable as the timer's constructor value (if you need to use context in the run method)

The following code is in the project. You can delete irrelevant code and change the skeleton to your own.

1 private final broadcastreceiver mconn_javaser = new broadcastreceiver (){
2 @ override
3 Public void onreceive (context, intent ){
4 string action = intent. getaction ();
5 If (descrithgatt. action_gatt_connected.equals (Action )){
6 // start the timer
7 proximityalarm (2, context );
8}
9}
10}
11
12 private final static string proximity_alarm_action = "com. cyberblue. iitag. proximityalarm ";
13 private proximityalarmrecevier preceiver;
14 // latency alert
15 public void proximityalarm (INT second, context ){
16 log. I (TAG, "Enter proximityalarm second =" + second );
17
18 if (preceiver = NULL ){
19 preceiver = new proximityalarmrecevier ();
20 registerreceiver (preceiver, new intentfilter (proximity_alarm_action ));
21}
22
23 timer time = new timer ();
24 proximityalarmtimertask task = new proximityalarmtimertask (context );
25 // dalay/1000 seconds later, execute the task only once.
26 time. Schedule (task, second * 1000 );
27}
28
29 public class proximityalarmtimertask extends timertask {
30 private context;
31 public proximityalarmtimertask (context mcontext ){
32 context = mcontext;
33}
34 public void run (){
35 log. I (TAG, "Enter proximityalarmtimertask ");
36 IF (mlestate = connected ){
37 return;
38} else {
39 intent = new intent (proximity_alarm_action );
40 sendbroadcast (intent );
41}
42}
43}
44
45 public class proximityalarmrecevier extends broadcastreceiver {
46 @ override
47 Public void onreceive (context, intent ){
48 log. I (TAG, "Enter proximityalarmrecevier ");
49 phandler. sendemptymessage (0 );
50}
51}
52
53 // process Reconnection
54 handler phandler = new handler (){
55 public void handlemessage (Message MSG ){
56 log. I (TAG, "Enter phandler ");
57 switch (msg. What ){
58 Case 0:
59 // stop the last music
60 stopmusic ();
61 // stop Vibration
62 cancelvibrator ();
63 // light up the screen
64 final powermanager. wakelock WL = wekelock ();
65 vibrator ();
66 playmusic ();
67 // only the last alarm window is retained
68 if (view! = NULL ){
69 WM. removeview (View );
70}
71 view = Inflater. Inflate (R. layout. proximity_result, null );
72 WM. addview (view, wmparams );
73 button proximitytagbtn = (button) view. findviewbyid (R. Id. proximitytagbtn );
74 proximitytagbtn. setonclicklistener (New onclicklistener (){
75 @ override
76 public void onclick (view v ){
77 // remove windowmanager
78 WM. removeview (View );
79 view = NULL;
80 // stop music
81 stopmusic ();
82 // stop Vibration
83 cancelvibrator ();
84 If (WL! = NULL & WL. isheld ()){
85 WL. Release ();
86}
87}
88 });
89 closewakelock (120, wl );
90
91 break;
92}
93 super. handlemessage (MSG );
94}
95 };
Related Article

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.