如何在片段类中使用发送广播方法

  • 本文关键字:广播 方法 片段 android
  • 更新时间 :
  • 英文 :


我需要一点帮助。我正在手机上尝试使用GPS。不知道这是否有效,但是在此之前,我已经为按钮设置了onclicklistener,但是它不允许使用sendBroadcast。任何人都有解决方案吗?

public class locate extends Fragment {
    Button b1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.locate, container, false);
    onCreate(savedInstanceState);
    b1=(Button)rootView.findViewById(R.id.widget33);
    b1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(Gloabal.getcontext(), "text", Toast.LENGTH_LONG).show();
            Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");
            intent.putExtra("enabled", true);
            sendBroadcast(intent);
            //startActivity(intent);
        }
    });
    return rootView;
}
}

任何其他使用意图的替代方案... m用可滑动的选项卡稍微粘住了一点,因此在此方面做了很多努力,任何帮助都非常感谢

您需要的是此代码:

   getActivity().sendBroadcast(intent);

编辑1:一些解释 ->将片段附加到活动上,这是sendbroadcast所需的上下文子类。因此,使用getActivity()。sendbroadcast()您将使用与活动相关的上下文。

编辑2:我在您的吐司中看到您使用的是gloabal.getContext(),用getActivity()!>

最新更新