Android 如何在BroadcastReceive的onReceive方法中设置TextView中的文本?
基本上,我想在BroadCaseReceiver中执行TextView类的setText()特性。但我无法实现这一点:我不知道Android 如何在BroadcastReceive的onReceive方法中设置TextView中的文本?,android,Android,基本上,我想在BroadCaseReceiver中执行TextView类的setText()特性。但我无法实现这一点:我不知道 为什么(context.findviewById()不工作),因为findviewById()是活动类的一部分,并且context.toString()显示活动(基本活动)类的引用?除非广播接收器是在活动的上下文中以编程方式启动的,例如Activity.onCreate(). 对于一个普通的广播接收器,从它访问像TextView这样的UI元素是非常奇怪的。您只能使用活动
为什么(
context.findviewById()
不工作),因为findviewById()
是活动类的一部分,并且context.toString()
显示活动(基本活动)类的引用?除非广播接收器是在活动的上下文中以编程方式启动的,例如Activity.onCreate(). 对于一个普通的广播接收器,从它访问像TextView这样的UI元素是非常奇怪的。您只能使用活动中的UI内容。也许你应该回顾一下你试图应用的解决方案。即使你成功地做到了这一点,它也会有一个错误的设计。下面的答案链接如下:
public class MyReceiver extends BroadcastReceiver {
private MyActivity myActivity;
public MyReceiver(MyActivity myActivity) {
this.myActivity= myActivity;
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d("", "Onrecieve ready to call");
if(this.myActivity!=null)
{
this.myActivity.update();
}
// make update method is in your activity.
// call function of your activity and change UI.
}
}
为了允许片段与其活动通信,您可以在片段类中定义一个接口,并在活动中实现它。片段在其onAttach()生命周期方法期间捕获接口实现,然后可以调用接口方法以便与活动通信
以下是片段到活动通信的示例:
公共类HeadlinesFragment扩展ListFragment{
OnHeadlineSelectedListener McCallback
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
...
}
现在,片段可以通过使用OnHeadlineSelectedListener接口的mCallback实例调用onArticleSelected()方法(或接口中的其他方法)向活动传递消息
例如,当用户单击列表项时,将调用片段中的以下方法。片段使用回调接口将事件传递给父活动
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.onArticleSelected(position);
}
实现接口
为了从片段接收事件回调,承载它的活动必须实现在片段类中定义的接口
例如,以下活动实现上述示例中的接口
公共静态类MainActivity扩展活动
实现HeadlinesFragment.OnHeadlineSelectedListener{
}
向片段传递消息
宿主活动可以通过使用findFragmentById()捕获片段实例,然后直接调用片段的公共方法,将消息传递给片段
例如,假设上面显示的活动可能包含另一个片段,用于显示上面回调方法中返回的数据指定的项。在这种情况下,活动可以将回调方法中接收到的信息传递给将显示该项的另一个片段:
公共静态类MainActivity扩展活动
实现HeadlinesFragment.OnHeadlineSelectedListener{
}很抱歉。。。但是我正在做一些类似于我使用(AsyncTask)从internet获取数据的事情,现在我想将这些数据发送到TextView,所以我使用broadcast_Receiver设置从internet获取的数据。您不需要BroadcastReceiver,然后…在onPostExecute()方法上完成所有操作…当然,确保您正在执行Background返回所需的数据…然后从专门为此情况设计的onPostExecute()中执行textView.setText()调用…它在UIThread上运行。这是做它的方式。你是真的。。。但是现在请认为我在片段中有一个片段,我现在有一个文本视图(没有片段你的答案),但是现在如果我有片段,那么如何在片段的_textView中设置我的文本。@user2147072如果你把你尝试过的代码和你现在想做的代码放在一起,会更好,我想你只是想将数据表单异步任务发送到你的活动,如果我是正确的,那么你不需要使用广播接收器。@AmitabhSarkar请看这个“你是对的……但是现在请认为我在片段中有一个片段我现在有一个文本视图(没有片段你的答案)但是现在如果我有fragment,那么如何在fragment的_textView中设置我的文本。“不好,在BroadcastReceiver中泄漏一个活动…活动可能会死亡,而BroadcastReceiver将泄漏一个死亡活动的引用…这是应用观察者模式的典型情况。如果活动死亡。。在
TextView
中设置文本不会出现问题。如果活动已停止,则取消注册reciver@Alecio哇,这是一个很好的答案,我从来没有这样想过。这是一个投票的答案。您已经使用了回调()的概念。
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
} else {
// Otherwise, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}