Event for gps on/off state change

﹥>﹥吖頭↗ 提交于 2019-12-02 05:51:40

问题


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?


回答1:


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");
                        }
                    }
             };

OR

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;
        }
    }
});


来源:https://stackoverflow.com/questions/20611824/event-for-gps-on-off-state-change

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!