0,0 → 1,96 |
/** |
* A simple notifier for Activities, encapsulating all the work necessary |
* to send a notification message on Android. |
*/ |
package de.pointedears.converter.helpers; |
|
import android.app.Activity; |
import android.app.Notification; |
import android.app.NotificationManager; |
import android.app.PendingIntent; |
import android.content.Context; |
import android.content.Intent; |
import de.pointedears.converter.R; |
import de.pointedears.converter.R.drawable; |
|
/** |
* Sends notification messages for {@link Activity Activities} |
* |
* @author Thomas 'PointedEars' Lahn |
*/ |
public class Notifier |
{ |
private static int nextId = 1; |
|
/** |
* |
*/ |
public Context activityContext; |
|
/** |
* @param activityContext |
* The activity for which a notification will be sent. This |
* allows the user to select the notification and go back |
* directly to the activity that issued it. |
* @param tickerText |
* The text to be displayed as notification, both as ticker message |
* and in the notification list. |
* @param notificationTitle |
* The title for the notification message. The default ( |
* <code>null</code>) |
* is the application name (R.strings.app_name). |
* @see Notifier#sendMessage(Context, CharSequence) |
*/ |
public static void sendMessage(Context activityContext, |
CharSequence tickerText, CharSequence notificationTitle) |
{ |
NotificationManager mNotificationManager = |
(NotificationManager) activityContext |
.getSystemService(Context.NOTIFICATION_SERVICE); |
|
Notification notification = |
new Notification(drawable.icon, tickerText, System.currentTimeMillis()); |
|
Context applicationContext = |
activityContext.getApplicationContext(); |
|
Intent notificationIntent = |
new Intent(activityContext, activityContext.getClass()); |
|
PendingIntent contentIntent = |
PendingIntent.getActivity(activityContext, 0, |
notificationIntent, 0); |
|
if (notificationTitle == null) |
{ |
notificationTitle = activityContext.getString(R.string.app_name); |
} |
|
CharSequence contentTitle = notificationTitle; |
CharSequence contentText = tickerText; |
|
notification.setLatestEventInfo(applicationContext, contentTitle, |
contentText, |
contentIntent); |
|
mNotificationManager.notify(Notifier.nextId, notification); |
++Notifier.nextId; |
} |
|
/** |
* Sends a notification message where the the application name |
* (R.strings.app_name) |
* is used as message title. |
* |
* @param activityContext |
* The activity for which a notification will be sent |
* @param tickerText |
* The text to be displayed as notification |
* @see Notifier#sendMessage(Context, CharSequence, CharSequence) |
*/ |
public static void sendMessage(Context activityContext, |
CharSequence tickerText) |
{ |
Notifier.sendMessage(activityContext, tickerText, null); |
} |
} |
Property changes: |
Added: svn:mime-type |
## -0,0 +1 ## |
+text/plain |
\ No newline at end of property |
Index: UpdateService.java |
=================================================================== |
--- UpdateService.java (nonexistent) |
+++ UpdateService.java (revision 17) |
@@ -0,0 +1,157 @@ |
+package de.pointedears.converter.helpers; |
+ |
+import android.app.Service; |
+import android.content.Intent; |
+import android.os.Handler; |
+import android.os.IBinder; |
+import de.pointedears.converter.app.CurrenciesActivity; |
+import de.pointedears.converter.net.RatesUpdater; |
+ |
+/** |
+ * Update service to run a thread to update currency rates |
+ * |
+ * @author Thomas 'PointedEars' Lahn |
+ */ |
+public class UpdateService extends Service |
+{ |
+ // private Timer myTimer = null; |
+ // private final BroadcastTimerTask sendTime = null; |
+ private static CurrenciesActivity activityContext; |
+ private ConverterThread updateThread; |
+ private Handler handler; |
+ private RatesUpdater ratesUpdater; |
+ |
+ public static final String ACTION_UPDATE = |
+ "de.pointedears.converter.ACTION_UPDATE"; //$NON-NLS-1$ |
+ public static final String EXTRA_ACTIVITY = |
+ "de.pointedears.converter.extra_activity"; //$NON-NLS-1$ |
+ public static final String EXTRA_NUM_RATES = |
+ "de.pointedears.converter.extra_num_rates"; //$NON-NLS-1$ |
+ public static final String EXTRA_DATE = "de.pointedears.converter.extra_date"; //$NON-NLS-1$ |
+ |
+ // private sendTimerTask sendTime = null; |
+ |
+ // /** inner class implements the broadcast timer */ |
+ // private class BroadcastTimerTask extends TimerTask |
+ // { |
+ // int counter = 0; |
+ // |
+ // @Override |
+ // public void run() |
+ // { |
+ // Intent intent = new Intent(UpdateService.ACTION_UPDATE); |
+ // String theTime = |
+ // "Time: " + System.currentTimeMillis() + ", Counter = " |
+ // + Integer.toString(this.counter); |
+ // this.counter++; |
+ // intent.putExtra("TIME", theTime); |
+ // UpdateService.this.sendBroadcast(intent); |
+ // } |
+ // }; |
+ |
+ @Override |
+ public IBinder onBind(Intent intent) |
+ { |
+ /* NOTE: Clients cannot bind to this service */ |
+ return null; |
+ } |
+ |
+ @Override |
+ public void onCreate() |
+ { |
+ super.onCreate(); |
+ // this.myTimer = new Timer("myTimer"); |
+ |
+ if (this.handler == null) |
+ { |
+ this.handler = new Handler(); |
+ } |
+ |
+ this.updateThread = null; |
+ } |
+ |
+ // /* |
+ // * (non-Javadoc) |
+ // * |
+ // * @see android.app.Service#onStartCommand(android.content.Intent, int, int) |
+ // */ |
+ // @Override |
+ // public int onStartCommand(Intent intent, int flags, int startId) |
+ // { |
+ // // TODO Auto-generated method stub |
+ // return super.onStartCommand(intent, flags, startId); |
+ // } |
+ |
+ @Override |
+ /** |
+ * @deprecated since SDK 2.0 |
+ */ |
+ public void onStart(Intent intent, int startId) |
+ { |
+ super.onStart(intent, startId); |
+ // this.myTimer.cancel(); |
+ // this.myTimer = new Timer("myTimer"); |
+ // this.sendTime = new BroadcastTimerTask(); |
+ // this.myTimer.scheduleAtFixedRate(this.sendTime, 0, 1000 * 5); |
+ |
+ String action = intent.getAction(); |
+ if (UpdateService.ACTION_UPDATE.equals(action)) |
+ { |
+ if (this.updateThread == null) |
+ { |
+ this.ratesUpdater = |
+ new RatesUpdater(UpdateService.getActivityContext(), this); |
+ this.updateThread = |
+ new ConverterThread(this.ratesUpdater, this.handler); |
+ this.ratesUpdater.setUpdateThread(this.updateThread); |
+ } |
+ |
+ try |
+ { |
+ this.updateThread.start(); |
+ // this.editValue1.setText("Gestartet!"); |
+ } |
+ catch (IllegalThreadStateException e) |
+ { |
+ // this.editValue1.setText("Bereits gestartet!"); |
+ } |
+ |
+ // case R.id.item_options_quit: |
+ // if (this.updateThread != null) |
+ // { |
+ // try |
+ // { |
+ // this.updateThread.join(); |
+ // } |
+ // catch (InterruptedException e) |
+ // { |
+ // // TODO Auto-generated catch block |
+ // } |
+ // |
+ // // this.editValue1.setText("Gestoppt -> Warten auf Start"); |
+ // } |
+ // else |
+ // { |
+ // // this.editValue1.setText("Bereits gestoppt -> Warten auf Start"); |
+ // } |
+ // return true; |
+ } |
+ } |
+ |
+ /** |
+ * @return the activityContext |
+ */ |
+ public static CurrenciesActivity getActivityContext() |
+ { |
+ return UpdateService.activityContext; |
+ } |
+ |
+ /** |
+ * @param activityContext |
+ * the activityContext to set |
+ */ |
+ public static void setActivityContext(CurrenciesActivity activityContext) |
+ { |
+ UpdateService.activityContext = activityContext; |
+ } |
+} |