Comment afficher une icône dans la barre d'état lorsque l'application est en cours d'exécution, y compris en arrière-plan?

StackOverflow https://stackoverflow.com/questions/3973208

Question

Je veux mettre une icône dans la barre d'état chaque fois que ma demande est en cours d'exécution, y compris lorsqu'il est en cours d'exécution en arrière-plan. Comment puis-je faire?

Était-ce utile?

La solution

Vous devriez être en mesure de le faire avec notification et la NotificationManager. Cependant obtenir un moyen sûr de savoir quand votre application ne fonctionne pas est le plus dur.

Vous pouvez obtenir la fonctionnalité de base de ce que vous êtes désireux de faire quelque chose comme:

Notification notification = new Notification(R.drawable.your_app_icon,
                                             R.string.name_of_your_app, 
                                             System.currentTimeMillis());
notification.flags |= Notification.FLAG_NO_CLEAR
                   | Notification.FLAG_ONGOING_EVENT;
NotificationManager notifier = (NotificationManager)
     context.getSystemService(Context.NOTIFICATION_SERVICE);
notifier.notify(1, notification);

Ce code doit être quelque part où vous êtes sûr aurez mis le feu au démarrage de votre application. Peut-être dans la méthode onCreate () de l'application de l'objet application personnalisée.

Cependant, après que les choses sont difficiles. Le meurtre de l'application peut se produire à tout moment. Ainsi, vous pouvez essayer de mettre quelque chose dans le OnTerminate () de la classe d'application aussi, mais il est pas garanti d'être appelé.

((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(1);

sera ce qui est nécessaire pour supprimer l'icône.

Autres conseils

Pour les nouvelles API, vous pouvez utiliser NotificationCompat.Builder -

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentTitle("Title");
Intent resultIntent = new Intent(this, MyActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(NOTIFICATION_ID, notification);

Il montrera aussi longtemps que votre application est en cours d'exécution et que quelqu'un ferme manuellement votre application. Vous pouvez toujours annuler votre notification en appelant -

mNotifyMgr.cancel(NOTIFICATION_ID);

Jetez un oeil au Guide Dev « Création Barre d'état Notifications ».

Une façon d'atteindre l'objectif de maintenir l'icône là que lorsque l'application est en cours d'exécution est d'initialiser la notification onCreate() et appel cancel(int) dans votre méthode de onPause() que si retourne isFinishing() vrai.

Exemple Un:

private static final int NOTIFICATION_EX = 1;
private NotificationManager notificationManager;

@Override
public void onCreate() {
    super.onCreate();

    notificationManager = (NotificationManager) 
        getSystemService(Context.NOTIFICATION_SERVICE);

    int icon = R.drawable.notification_icon;
    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = getApplicationContext();
    CharSequence contentTitle = "My notification";
    CharSequence contentText = "Hello World!";
    Intent notificationIntent = new Intent(this, MyClass.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 
        0, notificationIntent, 0);

    notification.setLatestEventInfo(context, contentTitle, 
        contentText, contentIntent);

    notificationManager.notify(NOTIFICATION_EX, notification);
}

@Override
protected void onPause() {
    super.onPause();
    if (isFinishing()) {
        notificationManager.cancel(NOTIFICATION_EX);
    }
}

Il fonctionne vraiment. J'ai créé une méthode de l'exemple ci-dessus:

private void applyStatusBar(String iconTitle, int notificationId) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(iconTitle);
Intent resultIntent = new Intent(this, ActMain.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_NO_CLEAR|Notification.FLAG_ONGOING_EVENT;

NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(notificationId, notification);}

Il devrait être appelé comme: applyStatusBar ( "Test Statusbar", 10);

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top