Rev 18 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
17 | PointedEar | 1 | package de.pointedears.converter.helpers; |
2 | |||
3 | import android.app.Service; |
||
4 | import android.content.Intent; |
||
5 | import android.os.Handler; |
||
6 | import android.os.IBinder; |
||
7 | import de.pointedears.converter.app.CurrenciesActivity; |
||
8 | import de.pointedears.converter.net.RatesUpdater; |
||
9 | |||
10 | /** |
||
11 | * Update service to run a thread to update currency rates |
||
12 | * |
||
13 | * @author Thomas 'PointedEars' Lahn |
||
14 | */ |
||
15 | public class UpdateService extends Service |
||
16 | { |
||
17 | // private Timer myTimer = null; |
||
18 | // private final BroadcastTimerTask sendTime = null; |
||
19 | private static CurrenciesActivity activityContext; |
||
20 | private ConverterThread updateThread; |
||
21 | private Handler handler; |
||
22 | private RatesUpdater ratesUpdater; |
||
23 | |||
24 | public static final String ACTION_UPDATE = |
||
25 | "de.pointedears.converter.ACTION_UPDATE"; //$NON-NLS-1$ |
||
26 | public static final String EXTRA_ACTIVITY = |
||
27 | "de.pointedears.converter.extra_activity"; //$NON-NLS-1$ |
||
28 | public static final String EXTRA_NUM_RATES = |
||
29 | "de.pointedears.converter.extra_num_rates"; //$NON-NLS-1$ |
||
30 | public static final String EXTRA_DATE = "de.pointedears.converter.extra_date"; //$NON-NLS-1$ |
||
31 | |||
32 | // private sendTimerTask sendTime = null; |
||
33 | |||
34 | // /** inner class implements the broadcast timer */ |
||
35 | // private class BroadcastTimerTask extends TimerTask |
||
36 | // { |
||
37 | // int counter = 0; |
||
38 | // |
||
39 | // @Override |
||
40 | // public void run() |
||
41 | // { |
||
42 | // Intent intent = new Intent(UpdateService.ACTION_UPDATE); |
||
43 | // String theTime = |
||
44 | // "Time: " + System.currentTimeMillis() + ", Counter = " |
||
45 | // + Integer.toString(this.counter); |
||
46 | // this.counter++; |
||
47 | // intent.putExtra("TIME", theTime); |
||
48 | // UpdateService.this.sendBroadcast(intent); |
||
49 | // } |
||
50 | // }; |
||
51 | |||
52 | @Override |
||
53 | public IBinder onBind(Intent intent) |
||
54 | { |
||
55 | /* NOTE: Clients cannot bind to this service */ |
||
56 | return null; |
||
57 | } |
||
58 | |||
59 | @Override |
||
60 | public void onCreate() |
||
61 | { |
||
62 | super.onCreate(); |
||
63 | // this.myTimer = new Timer("myTimer"); |
||
64 | |||
65 | if (this.handler == null) |
||
66 | { |
||
67 | this.handler = new Handler(); |
||
68 | } |
||
69 | |||
70 | this.updateThread = null; |
||
71 | } |
||
72 | |||
73 | // /* |
||
74 | // * (non-Javadoc) |
||
75 | // * |
||
76 | // * @see android.app.Service#onStartCommand(android.content.Intent, int, int) |
||
77 | // */ |
||
78 | // @Override |
||
79 | // public int onStartCommand(Intent intent, int flags, int startId) |
||
80 | // { |
||
81 | // // TODO Auto-generated method stub |
||
82 | // return super.onStartCommand(intent, flags, startId); |
||
83 | // } |
||
84 | |||
85 | @Override |
||
86 | /** |
||
87 | * @deprecated since SDK 2.0 |
||
88 | */ |
||
89 | public void onStart(Intent intent, int startId) |
||
90 | { |
||
91 | super.onStart(intent, startId); |
||
92 | // this.myTimer.cancel(); |
||
93 | // this.myTimer = new Timer("myTimer"); |
||
94 | // this.sendTime = new BroadcastTimerTask(); |
||
95 | // this.myTimer.scheduleAtFixedRate(this.sendTime, 0, 1000 * 5); |
||
96 | |||
97 | String action = intent.getAction(); |
||
98 | if (UpdateService.ACTION_UPDATE.equals(action)) |
||
99 | { |
||
100 | if (this.updateThread == null) |
||
101 | { |
||
102 | this.ratesUpdater = |
||
103 | new RatesUpdater(UpdateService.getActivityContext(), this); |
||
104 | this.updateThread = |
||
105 | new ConverterThread(this.ratesUpdater, this.handler); |
||
106 | this.ratesUpdater.setUpdateThread(this.updateThread); |
||
107 | } |
||
108 | |||
109 | try |
||
110 | { |
||
111 | this.updateThread.start(); |
||
112 | // this.editValue1.setText("Gestartet!"); |
||
113 | } |
||
114 | catch (IllegalThreadStateException e) |
||
115 | { |
||
116 | // this.editValue1.setText("Bereits gestartet!"); |
||
117 | } |
||
118 | |||
119 | // case R.id.item_options_quit: |
||
120 | // if (this.updateThread != null) |
||
121 | // { |
||
122 | // try |
||
123 | // { |
||
124 | // this.updateThread.join(); |
||
125 | // } |
||
126 | // catch (InterruptedException e) |
||
127 | // { |
||
128 | // // TODO Auto-generated catch block |
||
129 | // } |
||
130 | // |
||
131 | // // this.editValue1.setText("Gestoppt -> Warten auf Start"); |
||
132 | // } |
||
133 | // else |
||
134 | // { |
||
135 | // // this.editValue1.setText("Bereits gestoppt -> Warten auf Start"); |
||
136 | // } |
||
137 | // return true; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @return the activityContext |
||
143 | */ |
||
144 | public static CurrenciesActivity getActivityContext() |
||
145 | { |
||
146 | return UpdateService.activityContext; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param activityContext |
||
151 | * the activityContext to set |
||
152 | */ |
||
153 | public static void setActivityContext(CurrenciesActivity activityContext) |
||
154 | { |
||
155 | UpdateService.activityContext = activityContext; |
||
156 | } |
||
157 | } |