Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

android - Persistent service icon in notification bar

I am trying to figure out how to show that my service is running with a persistent notification icon. Many examples I've found only send a dismissable message to the notification bar. I wanted a persistent icon that when you pull down the notification bar it shows that the service is running and you can click it to open the app to shut the service down.

Can anyone point me to resources or tutorial on how to accomplish this any APK is fine but would like work with 4.0 and greater.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

it should be the same as a dismissable message except you change the Flag.

Notification.FLAG_ONGOING_EVENT

instead of

Notification.FLAG_AUTO_CANCEL

When a notification is clicked the intent you send is run, so you make sure that Activity does whatever task you want.

private void showRecordingNotification(){
    Notification not = new Notification(R.drawable.icon, "Application started", System.currentTimeMillis());
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, main.class), Notification.FLAG_ONGOING_EVENT);        
    not.flags = Notification.FLAG_ONGOING_EVENT;
    not.setLatestEventInfo(this, "Application Name", "Application Description", contentIntent);
    mNotificationManager.notify(1, not);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...