如何检测用户何时打开/关闭gps状态?

Dev*_*man 19 gps android broadcastreceiver

我想阻止用户更改我的应用程序中的WiFi,GPS和加载设置.用户在运行我的应用程序时无需打开/关闭WiFi和GPS.(来自通知栏).是否BroadcastReceiver存在用于收听GPS开/关的功能?

sud*_*007 32

好吧,我做了很多挖掘,发现addGpsStatusListener(gpsStatusListener)在API 24 中 已经弃用了.对我来说,这甚至都没有用!所以,这是另一个替代解决方案.

如果在您的应用中,您想要收听GPS状态更改(我的意思是用户开启/关闭).使用广播肯定是最好的方法.

执行:

/**
 * Following broadcast receiver is to listen the Location button toggle state in Android.
 */
private BroadcastReceiver mGpsSwitchStateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
            // Make an action or refresh an already managed state.
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

不要忘记在Fragment/Activity Lifecycle中有效注册和注销.

registerReceiver(mGpsSwitchStateReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
Run Code Online (Sandbox Code Playgroud)

例如,如果您使用此a Fragment,请在onResume注册中注册并取消注册onDestroy.此外,如果您要将用户指向设置以启用位置开关,则取消注册onStop将无法运行,因此,您的活动将进入onPause并且片段已停止.

那么这个解决方案可能有很多答案,但是这个解决方案很容易管理和使用.如果有的话,提出您的解决方案

  • @ sud007:展望未来,除非将其列入白名单,否则应用程序将不再收听广播接收器。因此,现在我们需要根据答案将接收者从清单声明更改为java / kotlin声明。现在,如果应用程序被杀死并且我打开了GPS,那么我没有其他方法可以让我的应用程序知道GPS处于打开/关闭状态,这就是我担心如果我遵循上述方法,我的应用程序将如何监听? (2认同)

lux*_*xer 22

您可以使用GpsStatus.Listener监听GPS状态,并使用LocationManager进行注册.

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;
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

您需要访问上下文(例如,在"Activity"或"Application"类中).

  • 所以从Nougat开始,`addGpsStatusListener在API级别24中被弃用.使用registerGnssStatusCallback(GnssStatus.Callback)代替.根据Android docs [here](https://developer.android.com/reference/android/location/LocationManager. HTML#addGpsStatusListener(android.location.GpsStatus.Listener)) (5认同)

mat*_*dev 7

LocationManager.PROVIDERS_CHANGED_ACTION在您的活动onResume()方法中收听事件:

IntentFilter filter = new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION);
filter.addAction(Intent.ACTION_PROVIDER_CHANGED);
mActivity.registerReceiver(gpsSwitchStateReceiver, filter);
Run Code Online (Sandbox Code Playgroud)

将此实例添加BroadcastReceiver到您的活动中:

private BroadcastReceiver gpsSwitchStateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
    
            if (LocationManager.PROVIDERS_CHANGED_ACTION.equals(intent.getAction())) {

                LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

                if (isGpsEnabled || isNetworkEnabled) {
                    // Handle Location turned ON
                } else {
                    // Handle Location turned OFF
                }
            }
        }
    };
Run Code Online (Sandbox Code Playgroud)

在您的活动onPause()方法中取消注册接收器:

mActivity.unregisterReceiver(gpsSwitchStateReceiver);
Run Code Online (Sandbox Code Playgroud)


MMG*_*MMG 5

Kotlin中试试这个:

添加一个扩展BroadcastReceiver的类:

class GPSCheck(private val locationCallBack: LocationCallBack) :
    BroadcastReceiver() {
    interface LocationCallBack {
        fun turnedOn()
        fun turnedOff()
    }

    override fun onReceive(context: Context, intent: Intent) {
        val locationManager =
            context.getSystemService(LOCATION_SERVICE) as LocationManager
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) locationCallBack.turnedOn() else locationCallBack.turnedOff()
    }

}
Run Code Online (Sandbox Code Playgroud)

然后作为示例以这种方式使用它:

class MainActivity :AppCompatActivity(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        registerReceiver(GPSCheck(object : GPSCheck.LocationCallBack {
            override fun turnedOn() {
                Log.d("GpsReceiver", "is turned on")
            }

            override fun turnedOff() {
                Log.d("GpsReceiver", "is turned off")
            }
        }), IntentFilter(LocationManager.MODE_CHANGED_ACTION))
    }}
Run Code Online (Sandbox Code Playgroud)


Jox*_*aex 3

这不可能。您无法随心所欲地控制/限制硬件的状态。这在 API 中存在是很危险的,因此不存在这样的 API。

  • 然后重新组织你的问题,因为那不是你要问的。您询问的是 **限制** 更改,而不是倾听它。 (3认同)