I want to create a notification that can be cleared.
e.g. Swipe right on ICS or by “Clear Notifications” button.
I can’t figure this out and my notification cannot be cleared.
I tried it both on Froyo & ICS and I got the same issue.
My code is given below:
public class UploadPhotoService extends Service {
private static final int SERVICE_ID = 999003;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification note = new Notification(R.drawable.stat_notify_chat,
"Uploading Photo", System.currentTimeMillis());
String filePath = intent.getStringExtra("filePath");
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(), 0);
note.setLatestEventInfo(this, "Uploading Photo", filePath, pi);
note.flags|=Notification.FLAG_AUTO_CANCEL;
startForeground(SERVICE_ID, note);
return(START_NOT_STICKY);
}
@Override
public void onDestroy() {
stop();
}
@Override
public IBinder onBind(Intent intent) {
return(null);
}
private void stop() {
stopForeground(true);
}
}
It’s because you are running a foreground service. Foreground service notifications stay up to alert users of a long running task. Drop the startForeground and it will work.
http://developer.android.com/reference/android/app/Service.html#startForeground(int,%20android.app.Notification)
If you want to show a notification just create one in the service, or on the correct events. Hope that helps.