How to send data to a running activity from Broadcast Receiver,

痞子三分冷 提交于 2019-12-03 14:42:01

First of all it's not the best idea to subscribe c2dm receiver in activity. Do it in manifest. For passing data to activity you can create static string field in Activity and set you String there.

You can do something like this:

in Activity

public static YourActivity mThis = null;
@Override
protected void onResume() {
    super.onResume();
    mThis = this;
}
@Override
protected void onPause() {
    super.onPause();
    mThis = null;
}

In your BroadcastReceiver:

@Override
public void onReceive(Context context, Intent intent) {
...
if (YourActivity.mThis != null) {
    ((TextView)YourActivity.mThis.findViewById(R.id.text)).setText("received c2dm");
}
else {
...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!