Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
17 | PointedEar | 1 | /** |
2 | * A simple notifier for Activities, encapsulating all the work necessary |
||
3 | * to send a notification message on Android. |
||
4 | */ |
||
5 | package de.pointedears.converter.helpers; |
||
6 | |||
7 | import android.app.Activity; |
||
8 | import android.app.Notification; |
||
9 | import android.app.NotificationManager; |
||
10 | import android.app.PendingIntent; |
||
11 | import android.content.Context; |
||
12 | import android.content.Intent; |
||
13 | import de.pointedears.converter.R; |
||
14 | import de.pointedears.converter.R.drawable; |
||
15 | |||
16 | /** |
||
17 | * Sends notification messages for {@link Activity Activities} |
||
18 | * |
||
19 | * @author Thomas 'PointedEars' Lahn |
||
20 | */ |
||
21 | public class Notifier |
||
22 | { |
||
23 | private static int nextId = 1; |
||
24 | |||
25 | /** |
||
26 | * |
||
27 | */ |
||
28 | public Context activityContext; |
||
29 | |||
30 | /** |
||
31 | * @param activityContext |
||
32 | * The activity for which a notification will be sent. This |
||
33 | * allows the user to select the notification and go back |
||
34 | * directly to the activity that issued it. |
||
35 | * @param tickerText |
||
36 | * The text to be displayed as notification, both as ticker message |
||
37 | * and in the notification list. |
||
38 | * @param notificationTitle |
||
39 | * The title for the notification message. The default ( |
||
40 | * <code>null</code>) |
||
41 | * is the application name (R.strings.app_name). |
||
42 | * @see Notifier#sendMessage(Context, CharSequence) |
||
43 | */ |
||
44 | public static void sendMessage(Context activityContext, |
||
45 | CharSequence tickerText, CharSequence notificationTitle) |
||
46 | { |
||
47 | NotificationManager mNotificationManager = |
||
48 | (NotificationManager) activityContext |
||
49 | .getSystemService(Context.NOTIFICATION_SERVICE); |
||
50 | |||
51 | Notification notification = |
||
52 | new Notification(drawable.icon, tickerText, System.currentTimeMillis()); |
||
53 | |||
54 | Context applicationContext = |
||
55 | activityContext.getApplicationContext(); |
||
56 | |||
57 | Intent notificationIntent = |
||
58 | new Intent(activityContext, activityContext.getClass()); |
||
59 | |||
60 | PendingIntent contentIntent = |
||
61 | PendingIntent.getActivity(activityContext, 0, |
||
62 | notificationIntent, 0); |
||
63 | |||
64 | if (notificationTitle == null) |
||
65 | { |
||
66 | notificationTitle = activityContext.getString(R.string.app_name); |
||
67 | } |
||
68 | |||
69 | CharSequence contentTitle = notificationTitle; |
||
70 | CharSequence contentText = tickerText; |
||
71 | |||
72 | notification.setLatestEventInfo(applicationContext, contentTitle, |
||
73 | contentText, |
||
74 | contentIntent); |
||
75 | |||
76 | mNotificationManager.notify(Notifier.nextId, notification); |
||
77 | ++Notifier.nextId; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Sends a notification message where the the application name |
||
82 | * (R.strings.app_name) |
||
83 | * is used as message title. |
||
84 | * |
||
85 | * @param activityContext |
||
86 | * The activity for which a notification will be sent |
||
87 | * @param tickerText |
||
88 | * The text to be displayed as notification |
||
89 | * @see Notifier#sendMessage(Context, CharSequence, CharSequence) |
||
90 | */ |
||
91 | public static void sendMessage(Context activityContext, |
||
92 | CharSequence tickerText) |
||
93 | { |
||
94 | Notifier.sendMessage(activityContext, tickerText, null); |
||
95 | } |
||
96 | } |