Monitoring Location Settings in a Library

一曲冷凌霜 提交于 2019-11-29 04:09:46
Daniel Nugent

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 its 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 receiveradded in its 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:

Donnie

Actually, you can use location.MODE_CHANGED and then register your receiver to receive the broadcast from it

<receiver android:name=".Receivers.LocationModeReceiver">
    <intent-filter>
        <action android:name="android.location.MODE_CHANGED" />
    </intent-filter>
</receiver>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!