2014-01-20 22 views
4

我需要一点帮助。我正在尝试使用移动设备上的gps。不知道是否有效,但在此之前,我已经为该按钮设置了onclicklistener,但它不允许发送传播。任何人都有解决方案吗?如何在片段类中使用发送广播方法

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卡使用滑动式标签有点为m这么多DONO周围,任何的帮助深表感谢

+0

[广播从片段到服务的意图](http://stackoverflow.com/questions/18687444/broadcasting-an-intent-from-a-fragment-to-a-service) –

回答

17

你需要的是这样的代码:

getActivity().sendBroadcast(intent); 

编辑1:一些解释 - >的片段连接到一个活动,这是背景信息的子类,需要sendBroadcast。因此,使用getActivity()。sendBroadcast(),您将使用与当前附加到Fragment的Activity相关的上下文。

编辑2:我在祝​​酒查看正在使用Gloabal.getcontext(),替换成getActivity()!

+0

那只是做配对,thanx – user3203211

+0

雅是完美的,理解:) – user3203211

+0

随时upvote和接受答案,如果它帮助;)它是什么让人们帮助和回答。 – JanBo

相关问题