Android Foreground Service 2013-04-12 13:24:36 public class MyService extends Service { public final String TAG = getClass().getName(); @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); Log.d(TAG, "service started"); Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); NotificationCompat.Builder notification = new NotificationCompat.Builder( getApplicationContext()).setSmallIcon(R.drawable.ic_launcher) .setTicker("hello ticker") .setContentTitle("hello title") .setContentText("hello text") .setContentIntent(pendingIntent).setAutoCancel(true) .setOngoing(true).setContentInfo(""); startForeground(42, notification.build()); return Service.START_STICKY; } @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onDestroy() { Log.d(TAG, "service stopped"); stopForeground(true); super.onDestroy(); } }