简体   繁体   中英

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.

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);
}

I know this is an old question, but it was first on a google results page so I'll add information to help others.

Persistent Notifications

The trick is to add .setOngoing to your NotificationCompat.Builder

Close Button

A button that opens the app and shuts the service down requires a PendingIntent

An Example

This example shows a persistent notification with a close button that exits the app.

MyService :

private static final int NOTIFICATION = 1;
public static final String CLOSE_ACTION = "close";
@Nullable
private NotificationManager mNotificationManager = null;
private final NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this);

private void setupNotifications() { //called in onCreate()
    if (mNotificationManager == null) {
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class)
                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
            0);
    PendingIntent pendingCloseIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class)
                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)
                    .setAction(CLOSE_ACTION),
            0);
    mNotificationBuilder
            .setSmallIcon(R.drawable.ic_notification)
            .setCategory(NotificationCompat.CATEGORY_SERVICE)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentTitle(getText(R.string.app_name))
            .setWhen(System.currentTimeMillis())
            .setContentIntent(pendingIntent)
            .addAction(android.R.drawable.ic_menu_close_clear_cancel,
                    getString(R.string.action_exit), pendingCloseIntent)
            .setOngoing(true);
}

private void showNotification() {
    mNotificationBuilder
            .setTicker(getText(R.string.service_connected))
            .setContentText(getText(R.string.service_connected));
    if (mNotificationManager != null) {
        mNotificationManager.notify(NOTIFICATION, mNotificationBuilder.build());
    }
}

MainActivity must handle the close intents.

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    String action = intent.getAction();
    if (action == null) {
        return;
    }
    switch (action) {
        case MyService.CLOSE_ACTION:
            exit();
            break;
    }
}    

private void exit() {
    stopService(new Intent(this, MyService.class));
    finish();
}

AnotherActivity must be finish and send an exit intent to MainActivity

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    String action = intent.getAction();
    if (action == null) {
        return;
    }
    switch (action) {
        case MyService.CLOSE_ACTION:
            exit();
            break;
    }
}

/**
 * Stops started services and exits the application.
 */
private void exit() {
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setAction(Stn1110Service.CLOSE_ACTION);
    startActivity(intent);
}

Can anyone point me to resources or tutorial

http://developer.android.com/training/notify-user/build-notification.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM