Skip to content Skip to sidebar Skip to footer

How To Use Startforeground?

I have an android service, I am using startForeground, and do not get what I to expect to get. I use the following code: Intent intent = new Intent(this, MainActivity.class);

Solution 1:

Write bellow code inside onStartCommand() method of your service:

Notification note = new Notification(R.drawable.ic_launcher,
            "Foreground Service notification?", System.currentTimeMillis());
    Intent i = new Intent(this, CurrentActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
    Date dateService=new Date(System.currentTimeMillis());
    String dateString=dateService.toString().split(" ")[1]+" "+dateService.toString().split(" ")[2]+" "+dateService.toString().split(" ")[3];
    note.setLatestEventInfo(this, "Foreground service",
            "Now foreground service running: "+dateString, pi);
    note.flags |= Notification.FLAG_AUTO_CANCEL;

    startForeground(2337, note);

Post a Comment for "How To Use Startforeground?"