Sto tentando di aggiornare l'interfaccia utente in FirstActivity
quando ricevo una notifica ma sono confuso da runOnUiThread
, Runnable
e Handler
. Ecco cosa ho: sto eseguendo FirstActivity e NotificationService. Quando NotificationService riceve una notifica, aggiornerà l'interfaccia utente di FirstActivity.Aggiornamento dell'interfaccia utente da un servizio (utilizzando un gestore?)
Ho anche un altro servizio AlarmService
in esecuzione. prima azione
@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
}
continuo a ricevere l'errore looper.prepare. Devo inserire codici extra nella mia FirstActivity?
puoi mostrarci i messaggi nel tuo log da logcat? Inoltre, come hai dichiarato Looper? –
Ricevo "Impossibile creare il gestore all'interno del thread che non ha chiamato errore Looper.prepare()". Looper è dichiarato nel metodo 'showNotification' sopra – newbie