监控Geofences用户打开位置服务关闭和打开后 [英] Monitoring Geofences after User Turns Location Service Off and On

查看:312
本文介绍了监控Geofences用户打开位置服务关闭和打开后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,可用于监控在Android设备上geofences库。我注意到,我注册与谷歌的geofences播放位置服务GeofencingApi类中我打开后,定位服务关闭并重新打开的设备设置丢失。

我已经看到了<一个href="http://stackoverflow.com/questions/30210862/android-geofence-not-triggers-once-location-off-and-on-again">folks这意味着我需要注册在我的的Andr​​oidManifest.xml 文件中的 android.location.PROVIDERS_CHANGED 广播接收器。我已经这么做了 - 但这个广播接收器只有被调用时,我在我的测试应用程序中添加和删除测试供应商。在设备设置之间进行切换位置服务的时候我不接受它。

我是不是做错了什么?有谁知道如何可靠地确定当用户切换位置服务的设备设置?我希望能够看到这些事件的背景 - 即使我的应用程序没有运行。

我想我可能会设置一个在后台运行,并定期检查,看看是否位置服务已打开的重复任务,但它听起来总值(GDP)和低效的。

如果有帮助,我测试的一个摩托摹运行Android 4.4.2。我的一切都做了与geofences一直很好,直到如今。

编辑:做更多的研究,我发现, PROVIDERS_CHANGED 广播的行为是高度可变取决于手机版本和型号。我的Nexus 5运行的是Android 5.1似乎做工精细其实 - 我能够获得 PROVIDERS_CHANGED 播出非常有规律。我也有一个摩托G和极限摩托手机上运行4.4.x,他们从来没有生产的 PROVIDERS_CHANGED 播出我。一个三星Galaxy S3在Android 4.2会产生广播的我,但停止这样做后,我用我的地图活动与测试位置提供商。

不管怎么说,我已经决定停止现在推行这个问题。我认为Android只是被车。

解决方案

这很难说没有看到您的code,如果你做了错事,但我只是有一个简单的例子工作,并通过使用$ C测试从$ C <一个href="http://stackoverflow.com/questions/15900180/is-it-possible-to-get-a-notification-when-any-location-provider-is-enabled-disab">here作为指导,并适应它有一个库中的BroadcastReceiver。

有一点需要注意:使用这个库的应用程序将需要有接收在它的Andr​​oidManifest.xml中。 请参阅<一href="http://stackoverflow.com/questions/6858070/how-to-declare-an-intent-filter-for-a-receiver-class-which-is-$p$psent-in-an-exte">here和这里,既包含@CommonsWare信息。

添加报价这里以防万一链接变坏:

  
    

有没有什么办法了库项目独立注册一个     接收器在它自己的manifest文件?

  
     

而不是在present时间。

所以,使用库中的任何应用程序将需要有接收在AndroidManifest.xml补充说。

请注意,这是在三星S4测试在Android 4.4.4。

我使用编译到一个库项目一个BroadcastReceiver该工作一个 AAR

使用接收元素设置在主应用程序,链接到的Andr​​oidManifest.xml中的的BroadcastReceiver 的图书馆,我能够接收事件时,任何变化作出的位置设置:

  • 在位置上切换
  • 位置切换关闭
  • 更改位置模式,以高精度
  • 更改位置模式为省电
  • 更改位置模式为仅GPS

每次变化是在所述位置设置,在库中的BroadcastReceiver被触发,即使该应用没有运行

我用一个简单的测试,其中的BroadcastReceiver库显示敬酒消息每个位置的设置被更改的时间。

下面是LocationProviderChangedReceiver.java库中的项目:

 进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.location.LocationManager;
进口android.util.Log;
进口android.widget.Toast;

公共类LocationProviderChangedReceiver扩展的BroadcastReceiver {
    私人最终静态字符串变量=LocationProviderChanged;

    布尔isGpsEnabled;
    布尔isNetworkEnabled;

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        如果(intent.getAction()。匹配(android.location.PROVIDERS_CHANGED))
        {
            Log.i(TAG,位置提供改);

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

            Toast.makeText(背景下,全球定位系统启用:+ isGpsEnabled +网络位置启用:+ isNetworkEnabled,Toast.LENGTH_LONG).show();
        }
    }

}
 

然后,在主要的应用程序,我把编译后的 myLibrary.aar 在libs文件夹,并设置build.gradle编译AAR:

 库{
        flatDir {
            迪尔斯'库'
        }
    }
}

依赖{
    编译文件树(导演:库,包括:['的* .jar'])
    编译com.android.support:appcompat-v7:22.1.1
    编译com.google.android.gms:玩-服务:7.3.0

    编译com.mylibrary.daniel:在MyLibrary:1.0@aar

}
 

设置了接收在主应用程序的Andr​​oidManifest.xml中:

 &LT;接收器
        机器人:名称=com.mylibrary.daniel.mylibrary.LocationProviderChangedReceiver
        机器人:出口=假&GT;
        &LT;意向滤光器&gt;
            &lt;作用机器人:名称=android.location.PROVIDERS_CHANGED/&GT;
            &LT;类机器人:名称=android.intent.category.DEFAULT/&GT;
        &所述; /意图滤光器&gt;
    &LT; /接收器&GT;
 

然后,安装应用程序,并在几个不同的方式修改的位置设置。在库中的BroadcastReceiver被触发,每次:

I'm working on a library that can be used to monitor geofences on Android devices. I've noticed that the geofences I register with Google Play Location Services GeofencingApi class are lost after I turn Location Services off and back on in the device settings.

I have seen folks suggesting that I need to register for the android.location.PROVIDERS_CHANGED broadcast receiver in my AndroidManifest.xml file. I've done that -- but this broadcast receiver only gets called when I add and remove test providers in my test application. I don't receive it at all when toggling Location Services in the device settings.

Am I doing something wrong? Does anyone know how to reliably determine when the user toggles Location Services in the device settings? I'd like to be able to see these events in the background -- even if my app is not running.

I figure I could set up an repeating task that runs in the background and periodically checks to see if Location Services has been toggled, but it sounds gross and inefficient.

If it helps, I'm testing on an Moto G running Android 4.4.2. Everything I have done with geofences has worked fine until now.

EDIT: After doing more research I have found that the behaviour of the PROVIDERS_CHANGED broadcast is highly variable depending on phone version and model. My Nexus 5 running Android 5.1 seems to work fine actually - I am able to get the PROVIDERS_CHANGED broadcast very regularly. I also have a Moto G and Moto X phone running 4.4.x and they never produced the PROVIDERS_CHANGED broadcast for me. A Samsung Galaxy S3 on Android 4.2 would produce the broadcast for me but stop doing it after I used my Map Activity with test location providers.

Anyways, I have decide to stop pursuing this problem for now. I think Android is just being buggy.

解决方案

It's hard to tell without seeing your code if you are doing anything wrong, but I just got a simple example working and tested by using the code from here as a guide, and adapting it to have the BroadcastReceiver in a library.

One thing to note: The app using the library will need to have the receiver in it's AndroidManifest.xml. See here and here, both contain information from @CommonsWare.

Adding quote here just in case the link goes bad:

Is there any way for a library project to independently register for a receiver in it's own manifest file?

Not at the present time.

So, any app that uses your library will need to have the receiver added in the AndroidManifest.xml.

Note that this was tested on Android 4.4.4 on a Samsung S4.

I got this working using a BroadcastReceiver in a library project compiled to a aar.

Using a receiver element set up in the AndroidManifest.xml of the main app that links to the BroadcastReceiver in the library, I was able to receive the event when any change is made to the Location settings:

  • Location toggle on
  • Location toggle off
  • Change Location Mode to High Accuracy
  • Change Location Mode to Power Saving
  • Change Location Mode to GPS only

Every time a change is made in the location settings, the BroadcastReceiver in the library is triggered, even if the app is not running.

I used a simple test where the BroadcastReceiver in the library shows a Toast message every time the location settings are changed.

Here is LocationProviderChangedReceiver.java in the library project:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;

public class LocationProviderChangedReceiver extends BroadcastReceiver {
    private final static String TAG = "LocationProviderChanged";

    boolean isGpsEnabled;
    boolean isNetworkEnabled;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED"))
        {
            Log.i(TAG, "Location Providers changed");

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

            Toast.makeText(context, "GPS Enabled: " + isGpsEnabled + " Network Location Enabled: " + isNetworkEnabled, Toast.LENGTH_LONG).show();
        }
    }

}

Then, in the main app, I put the compiled myLibrary.aar in the libs folder, and set up the build.gradle to compile the aar:

 repositories{
        flatDir{
            dirs 'libs'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile "com.android.support:appcompat-v7:22.1.1"
    compile "com.google.android.gms:play-services:7.3.0"

    compile 'com.mylibrary.daniel:mylibrary:1.0@aar'

}

Set up the receiver in the AndroidManifest.xml of the main app:

    <receiver
        android:name="com.mylibrary.daniel.mylibrary.LocationProviderChangedReceiver"
        android:exported="false" >
        <intent-filter>
            <action android:name="android.location.PROVIDERS_CHANGED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

Then, installed the app, and modified location settings in a few different ways. The BroadcastReceiver in the library was triggered every time:

这篇关于监控Geofences用户打开位置服务关闭和打开后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆