Wie ein Symbol in der Statusleiste angezeigt, wenn die Anwendung ausgeführt wird, im Hintergrund einschließlich?

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

Frage

Ich mag ein Symbol in der Statusleiste setzen, wann immer meine Anwendung ausgeführt wird, auch wenn es im Hintergrund läuft. Wie kann ich das tun?

War es hilfreich?

Lösung

Es soll möglich sein, dies zu tun mit Informationen und dem NotificationManager. Doch eine garantierte Art und Weise kennen zu lernen, wenn die Anwendung nicht ausgeführt wird, der schwierige Teil.

Sie können die grundlegenden Funktionen von dem, was Sie wünschen, durch wie etwas zu tun:

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

Dieser Code muss irgendwo sein, wo Sie sicher sind, werden beim Starten der Anwendung gefeuert. Möglicherweise in Ihrer Anwendung benutzerdefinierte Anwendungsobjekts onCreate () -Methode.

Doch nach, dass die Dinge heikel sind. Die Tötung der Anwendung kann jederzeit passieren. So können Sie versuchen, zu etwas in der OnTerminate () der Application-Klasse zu setzen, aber es ist nicht garantiert genannt werden.

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

wird sein, was benötigt wird, um das Symbol zu entfernen.

Andere Tipps

Für neue API können Sie NotificationCompat.Builder verwenden -

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

Es wird so lange zeigen, wie Ihre Anwendung ausgeführt wird und jemand schließt manuell Ihre Anwendung. Sie können Ihre Mitteilung durch den Aufruf immer stornieren -

mNotifyMgr.cancel(NOTIFICATION_ID);

Werfen Sie einen Blick auf dem Dev Guide „ Erstellen von Statusleiste Benachrichtigungen “.

Ein Weg, um das Ziel zu halten auf das Symbol nur zu erreichen, wenn die Anwendung ausgeführt wird, die Benachrichtigung in onCreate() und Anruf initialisieren cancel(int) in Ihrer onPause() Methode nur, wenn isFinishing() true zurück.

Ein Beispiel:

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

Es funktioniert wirklich. Ich habe ein Verfahren aus dem obigen Beispiel:

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

Es sollte so genannt werden: applyStatusBar ( "Statusbar Test", 10);

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top