在Fragment PageViewer中注册广播接收器

时间:2015-05-13 06:50:35

标签: android android-fragments android-viewpager broadcastreceiver android-manifest

我之前在Stack中问了一个问题,这是一个继续的问题

上一个问题

Calling Fragment from inside the BaseAdapter class in PageViewer Android

现在我点击按钮..它进入注册但似乎可以调用片段。 我想我需要注册。

如何在Fragment的清单文件中执行此操作 下面是我在Adapterclass中调用的广播接收器的代码

Fragment类中的代码和此mBroadcastReceiver传递给BaseAdapter类

 mBroadcastReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    Toast.makeText(context, "Recived", 
                               Toast.LENGTH_LONG).show();
                    FieldVisitFragment fieldvisitFragment = new FieldVisitFragment();
                    FragmentManager fragmentManager = getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.replace(android.R.id.tabcontent, fieldvisitFragment);
                    fragmentTransaction.commit();
                }

在适配器类的列表中单击按钮

我在BaseAdapter类中添加了代码

visitImg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Toast.makeText(con, "Visit Clicked ", Toast.LENGTH_LONG).show();
                con.registerReceiver(mBroadcastReceiver, new IntentFilter(
                        "start.fragment.action"));
            }
        });

用于在清单文件中注册

  <receiver android:name="what should I write here ???">
            <intent-filter>
                <action android:name="start.fragment.action"/>
            </intent-filter>
        </receiver>

在回答中看到但是仍然无效后所做的更改

清单文件

<receiver android:name="companyname.appname.fragments.FragmentClassName$BroadT">
        <intent-filter>
            <action android:name="start.fragment.action"/>
        </intent-filter>
    </receiver>

在Fragment类

中定义的BroadT类
public class FragmentClassName extends Fragment

BroadT课程

class BroadT extends BroadcastReceiver
      {
          @Override
                public void onReceive(Context context, Intent intent) {
                    Toast.makeText(context, "Recived", 
                               Toast.LENGTH_LONG).show();
                    FieldVisitFragment fieldvisitFragment = new FieldVisitFragment();
                    FragmentManager fragmentManager = getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager
                            .beginTransaction();
                    fragmentTransaction.replace(android.R.id.tabcontent, fieldvisitFragment);
                    fragmentTransaction.commit();
                }
      }

我在Base Adapter类中的调用

public void onClick(View arg0) {
                Toast.makeText(con, "Visit Clicked ", Toast.LENGTH_LONG).show();
                con.registerReceiver(mBroadcastReceiver, new IntentFilter(
                        "start.fragment.action"));
            }

但是我的<receiver android:name="packagename.BroadT"> BroadT类似乎没有链接到调用类

我觉得我需要使用NESTED FRAGMENTS任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

无需将片段注册到清单

你可以像这样从片段注册接收器

    private ScreenTimeOutReceiver mScreenTimeOutReceiver;

    mScreenTimeOutReceiver = new ScreenTimeOutReceiver();
    registerReceiver(mScreenTimeOutReceiver, filter);