本文介绍了gps开/关状态改变事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中收听 GPS 状态变化,在 android 中是否有任何意图操作,我可以在广播接收器中使用,例如用于网络的 android.net.conn.CONNECTIVITY_CHANGE -状态?

I want to listen a GPS state changes in my app, is there in android any intent-action which I can use in a broadCast receiver like an android.net.conn.CONNECTIVITY_CHANGE for network-state?

推荐答案

你可以注册一个BroadcastReceiver来监听Intent Action PROVIDERS_CHANGED_ACTION.或者你可以试试这个片段

You can register a BroadcastReceiver for listening to Intent Action PROVIDERS_CHANGED_ACTION.or you can try this snippet

GpsStatus.Listener gpsListener = new GpsStatus.Listener() {
                    public void onGpsStatusChanged(int event) {
                        if( event == GpsStatus.GPS_EVENT_FIRST_FIX){
                            showMessageDialog("GPS fixed");
                        }
                    }
             };

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.addGpsStatusListener(new android.location.GpsStatus.Listener()
{
    public void onGpsStatusChanged(int event)
    {
        switch(event)
        {
        case GPS_EVENT_STARTED:
            // do your tasks
            break;
        case GPS_EVENT_STOPPED:
            // do your tasks
            break;
        }
    }
});

这篇关于gps开/关状态改变事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:54