What is Foreground services ?

A foreground service is a service that should have the same priority as an active activity and therefore should not be killed by the Android system, even if the system is low on memory.

foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

Notification notification = new Notification(R.drawable.icon, 
                                             getText(R.string.ticker_text),
                                             System.currentTimeMillis());
 
Intent notificationIntent = new Intent(this, ExampleActivity.class); 
 
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0
                                             notificationIntent, 0); 
 
 
notification.setLatestEventInfo(this, getText(R.string.notification_title),
                                getText(R.string.notification_message), 
                                pendingIntent);
 
startForeground(ONGOING_NOTIFICATION_ID, notification); 

No comments:

Post a Comment