アクティビティでは、インテントをサービスに送信するときに を使用するだけ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) への静的参照を作成できません