0

アクティビティでは、インテントをサービスに送信するときに を使用するだけsendBroadcastですが、フラグメントは静的クラスでなければならないため、フラグメントから同じことを行うことはできません。これに対処する最善の方法は何ですか?

コンテキストを受け取るメソッドを持つことを検討しましたが、これはフラグメントの場合には機能しません。

現在、私のコードは

public static class ExampleSectionFragment extends Fragment {
    public static final String ARG_SECTION_NUMBER = "section_number";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_section_example, container, false);
        Bundle args = getArguments();

        //Example intent to broadcast to service on button press
        rootView.findViewById(R.id.action_start)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(RECORDING_INTENT);
                         sendBroadcast(intent);
                    }
                });
        return rootView;
    }
}

sendBroadcast は、Eclipse でエラーを返します。

タイプ ContextWrapper から非静的メソッド sendBroadcast(Intent) への静的参照を作成できません

4

1 に答える 1

4

Fragments はActivityのサブクラスである にアタッチされています -コール (外側のクラスを使用しようとしている)をContextに置き換えます- これは、現在アタッチされているに関連付けられているを使用します。sendBroadcastsendBroadcastgetActivity().sendBroadcastContextActivityFragment

于 2013-09-08T19:03:48.613 に答える