如何使用类片段发送广播法 [英] how to use send broadcast method in fragment class
问题描述
我需要一点点帮助。我想上一个移动的GPS。不知道是否是工作或没有,但在这之前我已经设置为onclicklistener按钮,但它不允许有sendbroadcast。任何人有这个解决方案?
I need a little bit of help. I'm trying on the gps on a mobile. No idea whether that works or not but before that I have set onclicklistener for the button but it does not allow to have sendbroadcast. Anyone have solution for this?
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卡使用滑动式标签让DONO多解决这个问题,任何帮助是非常AP preciated位为m
any other alternative to use the intent ...m stuck a bit as m using swipeable tabs so dono much around that,any help is much appreciated
推荐答案
您需要的是这个code:
getActivity().sendBroadcast(intent);
编辑1: 一些解释 - >片段连接到一个活动,这是上下文的子类,需要sendBroadcast。因此,与getActivity()。sendBroadcast(),您将使用与活动片段是目前连接到相关的上下文。的
编辑2: 我在你使用Gloabal.getcontext()你敬酒看到,替换与getActivity()!!! 的
这篇关于如何使用类片段发送广播法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!