Aktualisierung UI von a-service (mit einem handler?)

Ich versuche zu aktualisieren meine UI im FirstActivity wenn ich eine Benachrichtigung erhalten, aber ist verwirrt durch runOnUiThread , Runnable und Handler. Hier ist, was ich habe: ich arbeite mit FirstActivity und NotificationService. Wenn NotificationService reeives eine Benachrichtigung, aktualisiert FirstActivity UI.

Ich habe auch ein weiteres service AlarmService läuft.
Die Erste Aktivität

@Override
public void onResume() {
      super.onResume();
      //some other code for alarm service
}

NotificationService

    //on receiving notification
    private void showNotification(String text) {

   //Get activity
   Class<?> activityClass = null;
     try {
         activityClass = Class.forName("com.pakage.FirstActivity");
         contextActivity = (Activity) activityClass.newInstance();

         //Update UI on FirstActivity not working
         contextActivity.runOnUiThread(new Runnable() {
             public void run()
             { 
               Looper.prepare();
               TextView tv = (TextView ) contextActivity.findViewById(R.id.notifyTest);
               Looper.loop();

             }
             });

     } catch (Exception e) {
         e.printStackTrace();
     }

            //Shows the notification
            Notification n = new Notification();    
            //... etc   
}

Ich bekomme den Greifer.bereiten Sie Fehler. Muss ich extra codes in meinem FirstActivity?

können Sie uns zeigen, die Nachrichten in Ihrem Protokoll logcat? Auch, wie hast du das erklären Looper?
Ich bin immer Can't create handler inside thread that has not called Looper.prepare() Fehler. "Looper" ist deklariert in der showNotification Methode oben
Du bist immer, dass die Fehler da, Sie mit Ihrem code im UI-thread, der bereits über eine Looper-Lebenszyklus. Um zu vermeiden, dass Fehler, können Sie einfach entfernen Looper.prepare(), aber es macht nicht Sinn, da die `Looper.loop()' sperren der ganze thread und die Benutzeroberfläche Ihrer app würde weder Arbeit noch reagieren, nicht mehr!

InformationsquelleAutor newbie | 2011-10-29

Schreibe einen Kommentar