1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| public class StatusBarService extends Service {
private Timer _timer = null;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
_timer = new Timer();
_timer.schedule(new TimerTask() {
@Override
public void run() {
// Message message = new Message();
Bundle bundle = new Bundle();
bundle.putString("message_title", "Status bar notification");
bundle.putString("message_text", "Click here to fire intent");
bundle.putString("ticker_text", "Notification");
/*
message.obj = bundle;
toastHandler.sendMessage(message);
*/
/* Handler required only for toast notifications */
// Get reference to notification manager
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Instantiate the notification
Notification notification = new Notification(
R.drawable.icon, // The resource id of the icon to put in the status bar.
bundle.getString("ticker_text"), // The text that flows by in the status bar when the notification first activates.
System.currentTimeMillis()); // The time to show in the time field. In the System.currentTimeMillis timebase.
notification.flags = Notification.FLAG_AUTO_CANCEL;
// Set the expanded message and the intent to fire when the user clicks the expanded message
Intent notificationIntent = new Intent(
getApplicationContext(), // Application context.
StatusBarNotifications.class); // Activity to open.
PendingIntent contentIntent = PendingIntent.getActivity(
getApplicationContext(), 0, notificationIntent, 0);
notification.setLatestEventInfo(
getApplicationContext(),
bundle.getString("message_title"),
bundle.getString("message_text"),
contentIntent);
// Pass notification to notification manager
notificationManager.notify(
1, // Unique ID of notification
notification); // Notification
}
}, 5000);
_timer = new Timer();
_timer.schedule(new TimerTask() {
@Override
public void run() {
// Message message = new Message();
Bundle bundle = new Bundle();
bundle.putString("message_title", "Sasdasan");
bundle.putString("message_text", "Csdasdasent");
bundle.putString("ticker_text", "Nasdasion");
/*
message.obj = bundle;
toastHandler.sendMessage(message);
*/
/* Handler required only for toast notifications */
// Get reference to notification manager
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Instantiate the notification
Notification notification = new Notification(
R.drawable.icon, // The resource id of the icon to put in the status bar.
bundle.getString("ticker_text"), // The text that flows by in the status bar when the notification first activates.
System.currentTimeMillis()); // The time to show in the time field. In the System.currentTimeMillis timebase.
notification.flags = Notification.FLAG_AUTO_CANCEL;
// Set the expanded message and the intent to fire when the user clicks the expanded message
Intent notificationIntent = new Intent(
getApplicationContext(), // Application context.
StatusBarNotifications.class); // Activity to open.
PendingIntent contentIntent = PendingIntent.getActivity(
getApplicationContext(), 0, notificationIntent, 0);
notification.setLatestEventInfo(
getApplicationContext(),
bundle.getString("message_title"),
bundle.getString("message_text"),
contentIntent);
// Pass notification to notification manager
notificationManager.notify(
1, // Unique ID of notification
notification); // Notification
}
}, 5000);
return super.onStartCommand(intent, flags, startId);
} |
Partager