1. foxylion, 17.08.2010 #1
    foxylion

    foxylion Threadstarter Android-Hilfe.de Mitglied

    Hallo,

    ich hab eine Notification mit eigenem Layout und würde jetzt gerne die Notification immer mal wieder abändern, ohne sie zu löschen und neu zu erstellen. Wie mach ich das? Alle versuche den Text zu ändern sind bis jetzt daran gescheitert, das die Anzeige der Notification nicht aktualisiert wird.

    Code:
    Code:
    package de.foxylion.android.test;
    
    import android.app.Activity;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.RemoteViews;
    
    public class PermanentNotification {
        
        private static String uniqueText = "string";
        private static int uniqueId = 1473513389;
        
        private Activity activity;
        private NotificationManager mManager;
        private Notification notification;
        private RemoteViews contentView;
        
        public PermanentNotification(Activity activity) {
            this.activity = activity;
            
            mManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        
        public void show() {
            NotificationManager mManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
            
            notification = new Notification(R.drawable.icon,
                    activity.getString(R.string.app_name),
                    System.currentTimeMillis());
            
            contentView = new RemoteViews(activity.getPackageName(), R.layout.permanent_notification);
            setText(activity.getString(R.string.app_loading));
            notification.contentView = contentView;
            
            Intent notificationIntent = new Intent(activity, activity.getClass());
            notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            PendingIntent contentIntent = PendingIntent.getActivity(activity, 0, notificationIntent, 0);
            notification.contentIntent = contentIntent;
            
            notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
            
            mManager.notify(uniqueText, uniqueId, notification);
        }
        
        public void hide() {
            mManager.cancel(uniqueText, uniqueId);
        }
        
        public void setText(String text) {
            contentView.setTextViewText(R.id.PermanentNotification_Text, text);
        }
    }
    
    Layout:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical">
      
         <ImageView android:id="@+id/PermanentNotification_Image"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="10dp"
                  android:layout_marginTop="10dp"
                  android:layout_marginRight="10dp"
                  
                  android:src="@drawable/icon"
                  />
                  
        <TextView android:id="@+id/PermanentNotification_Heading"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="65dp"
                  android:layout_marginTop="6dp"
                  
                  android:textColor="#555555"
                  android:textStyle="bold"
                  
                  android:text="@string/app_name"
                  />
                  
        <TextView android:id="@+id/PermanentNotification_Text"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="65dp"
                  android:layout_marginTop="20dp"
                  
                  android:textColor="#000000"
                  />
    </RelativeLayout>
    Gestartet wird das ganze, indem Man ein Objekt der Klasse anlegt und die Acitivity die dazugehört mitgibt.
    setText ändert aber leider nicht den Text der Notification.

    Jemand ne Idee?

    Grüße & Danke schon mal
    foxyyy
     
    Zuletzt bearbeitet: 17.08.2010
  2. foxylion, 18.08.2010 #2
    foxylion

    foxylion Threadstarter Android-Hilfe.de Mitglied

    Okay, hab eine einfache Lösung gefunden, man darf nicht nur den Text verändern sondern muss auch eine neue Notification callen.
    Kleines Beispiel, falls es jemand mal braucht:

    Test.java (Main Activity)
    Code:
    package de.foxylion.android.test;
    
    import android.app.Activity;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.RemoteViews;
    
    public class Test extends Activity {
        
        RemoteViews contentView;
        Notification notification;
        NotificationManager mNotificationManager;
        
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            
            String ns = Context.NOTIFICATION_SERVICE;
            mNotificationManager = (NotificationManager) getSystemService(ns);
    
            int icon = R.drawable.icon;
            CharSequence tickerText = "Hello";
            long when = System.currentTimeMillis();
    
            notification = new Notification(icon, tickerText, when);
            
            contentView = new RemoteViews(getPackageName(), R.layout.permanent_notification);
            contentView.setTextViewText(R.id.PermanentNotification_Text, "App lädt" + Math.random());
            
            notification.contentView = contentView;
            
            Intent notificationIntent = new Intent(this, getClass());
            notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
            notification.contentIntent = contentIntent;
    
            notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
            mNotificationManager.notify(1, notification);
            
            Thread t = new SimpleThread(contentView, notification, mNotificationManager);
            t.start();
        }
    }
    SimpleThread.java
    Code:
    package de.foxylion.android.test;
    
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.widget.RemoteViews;
    
    public class SimpleThread extends Thread {
        
        private RemoteViews rv;
        private Notification not;
        private NotificationManager mng;
        
        public SimpleThread(RemoteViews rv, Notification notification, NotificationManager mNotificationManager) {
            this.rv = rv;
            this.not = notification;
            this.mng = mNotificationManager;
        }
        
        public void run() {
            while(true) {
                rv.setTextViewText(R.id.PermanentNotification_Text, "App lädt" + Math.random());
                mng.notify(1, not);
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
     
  3. the_alien, 18.08.2010 #3
    the_alien

    the_alien Android-Lexikon

    Eigentlich musst du nur die Notification ändern und ein notify aufrufen mit der gleichen ID wie vorher ;)
     
  4. foxylion, 18.08.2010 #4
    foxylion

    foxylion Threadstarter Android-Hilfe.de Mitglied

    Jou, mach ich ja nu ;-)