Event for gps on/off state change

前端 未结 1 1007
無奈伤痛
無奈伤痛 2021-01-22 07:45

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<

相关标签:
1条回答
  • 2021-01-22 08:01

    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;
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题