new*_*bie 5 service user-interface android handler runnable
我试图更新我的UI FirstActivity当我收到通知,但被迷惑runOnUiThread,Runnable和Handler.这就是我所拥有的:我正在运行FirstActivity和NotificationService.当NotificationService收到通知时,它将更新FirstActivity UI.
我还有另一个服务AlarmService正在运行.第一项活动
@Override
public void onResume() {
super.onResume();
//some other code for alarm service
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
我一直在搞looper.prepare错误.我是否需要在FirstActivity中添加额外的代码?
我的第一直觉是,您应该将Activity绑定到您的服务并在其一侧处理UI更新,而不是直接修改Activity的Service.
在此处查看更多信息:http:
//developer.android.com/reference/android/app/Service.html#LocalServiceSample
这里有一个例子:
示例:使用Messaging在Activity和Service之间进行通信
| 归档时间: |
|
| 查看次数: |
28374 次 |
| 最近记录: |