Event for gps on/off state change

2019-08-01 08: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?

标签: android gps
1条回答
劫难
2楼-- · 2019-08-01 09:17

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;
        }
    }
});
查看更多
登录 后发表回答