Come mostrare un'icona nella barra di stato quando l'applicazione è in esecuzione, tra cui in background?

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

Domanda

Voglio mettere un'icona nella barra di stato, quando mai la mia applicazione è in esecuzione, anche quando è in esecuzione in background. Come posso fare questo?

È stato utile?

Soluzione

Si dovrebbe essere in grado di fare questo con notifica e la NotificationManager. Tuttavia ottenere un modo garantito per sapere quando l'applicazione non è in esecuzione è la parte più difficile.

È possibile ottenere la funzionalità di base di che cosa state desiderando facendo qualcosa di simile:

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

Questo codice deve essere da qualche parte dove si è sicuri sarà licenziato all'avvio del applicativi. Possibilmente in modo onCreate () dell'applicazione di oggetti applicativi personalizzati.

Tuttavia dopo che le cose sono difficili. L'uccisione della domanda può avvenire in qualsiasi momento. Così si può provare a mettere qualcosa nel onTerminate () della classe Application troppo, ma non è garantito di essere chiamato.

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

sarà ciò che è necessario per rimuovere l'icona.

Altri suggerimenti

Per la nuova API è possibile utilizzare 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);

Si mostrerà il tempo che l'applicazione è in esecuzione e qualcuno chiude manualmente l'applicazione. È sempre possibile annullare la notifica chiamando -

mNotifyMgr.cancel(NOTIFICATION_ID);

Date un'occhiata alla guida Dev " Creazione Barra di stato Notifiche ".

Un modo per raggiungere l'obiettivo di mantenere l'icona non solo quando l'applicazione è in esecuzione è di inizializzare la notifica nella onCreate() e call cancel(int) nel metodo onPause() solo se i rendimenti isFinishing() vero.

Un esempio:

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

Funziona davvero. Ho creato un metodo fuori l'esempio precedente:

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

Dovrebbe essere chiamato come: applyStatusBar ( "Barra di stato Test", 10);

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top