Android: detect when GPS is turned on/off (or when no app is using it anymore)

前端 未结 4 1478
太阳男子
太阳男子 2021-02-13 02:24

I\'m on Android 2.3.7 and I\'m trying to write a BroadcastReceiver that will get called when the user turns GPS on/off (ie: by tapping on the \"use GPS satellites\" in the optio

4条回答
  •  礼貌的吻别
    2021-02-13 02:44

    The following solution might be a weird solution but you can try it if you don't find any solution. Another problem with your solution is it works only after api level 9.

    in a thread or service you can check the gps status periodically with a very short interval by using

     LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
     boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    

    it will use the permission

    android.permission.ACCESS_FINE_LOCATION
    

    Even though I am answering this, to me it should not be a good solution. Also there is a chance of minimum period interval delay. Just an option.

提交回复
热议问题