[英]How to update TextView with onReceive method from BroadcastReceiver
[英]How to setText in TextView inside onReceive method of BroadCastReceiver?
基本上我想在其中執行TextView類的setText()功能BroadCaseReceiver。 但是我無法實現這一點:我不知道
為什么( context.findviewById()
無法正常工作),因為findViewById()
是Activity類的一部分,而context.toString()
顯示的是Activity(基礎活動)類的引用?
鏈接到以下答案在這里: 與其他片段進行通信
為了允許一個Fragment與其Activity進行通信,您可以在Fragment類中定義一個接口,並在Activity中實現它。 片段在其onAttach()生命周期方法中捕獲接口實現,然后可以調用Interface方法以與Activity通信。
這是“片段到活動”通信的示例:
公共類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();
}
}
}
除非廣播廣播接收器是在Activity的上下文中以編程方式啟動的,例如Activity.onCreate()。 對於普通的BroadcastReceiver,從其中訪問UI元素(如TextView)非常奇怪。 您只能使用活動中的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.
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.