Android:检测用户打开状态栏

发布于 2024-12-08 08:01:13 字数 518 浏览 0 评论 0原文

我正在编写一个使用 GPS 进行定位服务的应用程序。当用户尝试在禁用 GPS 的情况下使用该应用程序时,会弹出一个对话框。现在,用户可以最小化应用程序并更改 GPS 设置。当他重新打开应用程序时,它会在 onResume() 中检测到设置已更改并关闭对话框。 这工作正常,但用户还有第二种启用 GPS 的方法:状态栏。当状态栏打开和关闭时, onResume() 和 onWindowFocusChanged() 都不会被调用,因此我不会检测用户何时启用 GPS。

有没有办法检测状态栏的打开?

我可以通过 LocationListener 来做到这一点,但我想按照我上面写的方式来做到这一点。

提前致谢!


我在 android-dev IRC 找到了帮助。现在,我使用观察者来注意设置更改,例如此处。因此,我会检测所有设置更改,即使它们是通过通知栏完成的,而不需要从 LocationManager 请求更新。 感谢您的帮助!

I am writing an app that uses GPS for localization services. When the user tries to use the app with a disabled GPS, a dialog pops up. Now, the user can minimize the app and change the GPS settings. When he reopens the app, it detects in onResume() that the settings were changed and closes the dialog.
That works fine, but the user has a second way to enable GPS: the status bar. When the status bar is opened and closed, neither onResume() nor onWindowFocusChanged() gets called, so that I don't detect when the user enabled GPS.

Is there a way to detect the opening of the status bar?

I could do it via the LocationListener, but I would like to do it how I wrote above.

Thanks in advance!


I found help at android-dev IRC. Now, I am using an Observer to notice setting changes like here. So, I detect all setting changes, even if they are done via the notifications bar, without requesting updates from the LocationManager.
Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北恋 2024-12-15 08:01:13

要监听用户已启用的 GPS,使用状态栏,您可以通过位置管理器设置监听器以进行更改:

private class DisabledGPSListener implements LocationListener{

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        locationManager.removeUpdates(this);
        locationManager.addGpsStatusListener(gpsStatusListener);
    }

在 onCreate() 内设置此监听器

if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
    locationManager.addGpsStatusListener(gpsStatusListener);
}else{
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, new DisabledGPSListener());
}

,其中 gpsStatusListener 是以下实例:

    private class GPSStatuslistener implements GpsStatus.Listener {

    @Override
    public void onGpsStatusChanged(int event) {
        switch (event) {
        case GpsStatus.GPS_EVENT_STARTED:
            Log.d(TAG, "ongpsstatus changed started");
            //TODO: your code that get location updates, e.g. set active location listener 
            break;
        case GpsStatus.GPS_EVENT_STOPPED:
            Log.d(TAG, "ongpsstatus changed stopped");
            createGpsDisabledAlert();
        }
    }

For listening to GPS that user has enabled, using a status bar, you can setup a listener for changes via the location manager:

private class DisabledGPSListener implements LocationListener{

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        locationManager.removeUpdates(this);
        locationManager.addGpsStatusListener(gpsStatusListener);
    }

set this listener inside onCreate()

if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
    locationManager.addGpsStatusListener(gpsStatusListener);
}else{
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, new DisabledGPSListener());
}

where gpsStatusListener is instance of:

    private class GPSStatuslistener implements GpsStatus.Listener {

    @Override
    public void onGpsStatusChanged(int event) {
        switch (event) {
        case GpsStatus.GPS_EVENT_STARTED:
            Log.d(TAG, "ongpsstatus changed started");
            //TODO: your code that get location updates, e.g. set active location listener 
            break;
        case GpsStatus.GPS_EVENT_STOPPED:
            Log.d(TAG, "ongpsstatus changed stopped");
            createGpsDisabledAlert();
        }
    }
吲‖鸣 2024-12-15 08:01:13

您正在寻找的是 LocationManager.addGpsStatusListener 吗?

Is it LocationManager.addGpsStatusListener you are looking for?

西瑶 2024-12-15 08:01:13

这并不能直接解决您的问题,但您可能会发现为用户打开设置更容易:

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);

This doesn't directly address your issue, but you may find it easier to open the settings for the user:

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文