基本上我想在BroadCaseReceiver中执行TextView类的setText()功能。但我无法实现这一点:我不知道
为什么(context.findviewById()
不起作用),因为findViewById()
是Activity类的一部分而context.toString()
显示了Activity(基本活动)类的引用?
答案 0 :(得分:2)
以下答案的链接位于:Communicating with Other Fragments
要允许Fragment与其Activity进行通信,您可以在Fragment类中定义接口并在Activity中实现它。 Fragment在其onAttach()生命周期方法中捕获接口实现,然后可以调用Interface方法以与Activity通信。
以下是片段到活动通信的示例:
public class HeadlinesFragment扩展ListFragment { OnHeadlineSelectedListener mCallback;
// 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扩展了Activity 实现HeadlinesFragment.OnHeadlineSelectedListener { ...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
} 向片段发送消息 主机活动可以通过使用findFragmentById()捕获Fragment实例来将消息传递给片段,然后直接调用片段的公共方法。
例如,假设上面显示的活动可能包含另一个片段,该片段用于显示上述回调方法中返回的数据指定的项目。在这种情况下,活动可以将回调方法中收到的信息传递给将显示该项目的另一个片段:
公共静态类MainActivity扩展了Activity 实现HeadlinesFragment.OnHeadlineSelectedListener { ...
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();
}
}
}
答案 1 :(得分:0)
除非在活动的上下文中以编程方式启动BroadcastReceiver,例如Activity.onCreate()。对于普通的BroadcastReceiver,从中访问像TextView这样的UI元素是非常奇怪的。您只能使用活动中的UI内容。也许您应该查看您尝试应用的解决方案。即使你设法做了这样的事情,也会有错误的设计。
答案 2 :(得分:0)
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.
}
}