从 BroadCastReceiver 更新 UI
update UI from BroadCastReceiver
如何从 BroadCastReceiver 更新主线程。我有一个接收字符串的 BroadCastReceiver,我需要在我的 MainActivity 上设置文本视图的文本,当我在 onReceive 方法上执行 findViewById(R.id.textView);
它 returns null.
这是我的 MainActivity onCreate。
public class MainActivity extends Activity {
......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter broadcastFilter = new IntentFilter(ResponseReceiver.LOCAL_ACTION);
receiver = new ResponseReceiver();
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
localBroadcastManager.registerReceiver(receiver, broadcastFilter);
Intent msgIntent = new Intent(MainActivity.this, IntentService.class);
msgIntent.putExtra(StockDataIntentService.PARAM_IN_MSG, getResources().getString(R.string.url));
startService(msgIntent);
}
public class ResponseReceiver extends BroadcastReceiver {
public static final String LOCAL_ACTION = "com.mypackage.ALL_DONE";
private Handler handler;
@Override
public void onReceive(Context context, Intent intent) {
String text =intent.getStringExtra(IntentService.PARAM_OUT_MSG);
TextView txtError = (TextView) findViewById(R.id.error);
}
}
此 question was having exact same issue, the accepted answer explains to use a handler etc.. But reading the comments on the answer explain the BroadastReceiver runs on the main thread 的 OP,因此没有理由使用处理程序。为什么我无法在文本视图上设置文本?
when I do findViewById(R.id.textView); on the onReceive method it
returns null.
因为目前没有调用 Activity 的 onCreate
方法中的 setContentView
方法来设置当前 Activity
的布局
因此,在 onCreate 中调用 setContentView
,方法是传递布局,其中 error
TextView 可用
如何从 BroadCastReceiver 更新主线程。我有一个接收字符串的 BroadCastReceiver,我需要在我的 MainActivity 上设置文本视图的文本,当我在 onReceive 方法上执行 findViewById(R.id.textView);
它 returns null.
这是我的 MainActivity onCreate。
public class MainActivity extends Activity {
......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter broadcastFilter = new IntentFilter(ResponseReceiver.LOCAL_ACTION);
receiver = new ResponseReceiver();
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
localBroadcastManager.registerReceiver(receiver, broadcastFilter);
Intent msgIntent = new Intent(MainActivity.this, IntentService.class);
msgIntent.putExtra(StockDataIntentService.PARAM_IN_MSG, getResources().getString(R.string.url));
startService(msgIntent);
}
public class ResponseReceiver extends BroadcastReceiver {
public static final String LOCAL_ACTION = "com.mypackage.ALL_DONE";
private Handler handler;
@Override
public void onReceive(Context context, Intent intent) {
String text =intent.getStringExtra(IntentService.PARAM_OUT_MSG);
TextView txtError = (TextView) findViewById(R.id.error);
}
}
此 question was having exact same issue, the accepted answer explains to use a handler etc.. But reading the comments on the answer explain the BroadastReceiver runs on the main thread 的 OP,因此没有理由使用处理程序。为什么我无法在文本视图上设置文本?
when I do findViewById(R.id.textView); on the onReceive method it returns null.
因为目前没有调用 Activity 的 onCreate
方法中的 setContentView
方法来设置当前 Activity
因此,在 onCreate 中调用 setContentView
,方法是传递布局,其中 error
TextView 可用