我正在尝试在接收到通知时更新
我一直收到looper.prepare错误的提示。我需要在我的FirstActivity中添加额外的代码吗?
FirstActivity
的用户界面,但是对于runOnUiThread
、Runnable
和Handler
感到困惑。以下是我的代码:我运行FirstActivity
和NotificationService
。当NotificationService
收到通知时,它将更新FirstActivity
的用户界面。
我还有另一个服务AlarmService
在运行。
@Override
public void onResume() {
super.onResume();
//some other code for alarm service
}
通知服务
//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
}
我一直收到looper.prepare错误的提示。我需要在我的FirstActivity中添加额外的代码吗?
Looper.prepare()
,但这样做没有任何意义,因为Looper.loop()
会阻塞整个线程,你的应用程序的UI将不再工作或响应! - Davide Cannizzo