Android 从c2dm事件侦听器更新活动文本区域
我想在触发c2dm事件时更新布局元素(TextArea),如何从事件侦听器获取视图 事件侦听器中,两行感兴趣的内容位于消息处理、更新文本区域注释下Android 从c2dm事件侦听器更新活动文本区域,android,Android,我想在触发c2dm事件时更新布局元素(TextArea),如何从事件侦听器获取视图 事件侦听器中,两行感兴趣的内容位于消息处理、更新文本区域注释下 public class C2DMMessageReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Strin
public class C2DMMessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("C2DM", "Message Receiver called");
if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
Log.w("C2DM", "Received message");
final String payload = intent.getStringExtra("payload");
Log.d("C2DM", "dmControl: payload = " + payload);
// Message handling, update text area
if(payload.equals("DataUpdate")) {
t=(TextView) context.findViewById(R.id.gameConsoleText);
t.setText("evt rcvd!");
}
}
}
}
此行有错误,我无法从上下文获取findViewByID
t=(TextView) context.findViewById(R.id.gameConsoleText);
有什么想法吗?谢谢findViewById()只存在于活动中
但你可以用这样的方法:
LayoutInflater myInflater = LayoutInflater.from(context);
View myView = myInflater.inflate(R.layout.yourView, null);
t = (TextView) myView.findViewById(R.id.gameConsoleText);
但您可能应该提出在活动中更新UI的意图