How to provide your own LocationProvider on Android?

前端 未结 4 480
一生所求
一生所求 2020-12-29 14:41

Does anyone know how to provide your own LocationProvider and publish it to the system, so other apps may fetch a reference to it from the LocationManager?

4条回答
  •  囚心锁ツ
    2020-12-29 15:32

    For development and testing purposes you can implement a mock location provider. You need to enable "Allow mock locations" in developer options in order for applications to see locations from that provider.

    For a code example, look at GPX Playback:
    https://github.com/johncarpenter/Android-GPX-Mock-Location-Provider

    The code for registering the location provider starts in line 203 of file android/src/com/twolinessoftware/android/PlaybackService.java.

    If you want a "real" location provider (e.g. to deploy your functionality as part of an end-user application), some sources say it cannot be done unless that provider is in a package signed with the system key, so you'd have to build your own ROM from source, sign it with your key, sign your location provider with the same key and install both on your phone.

    However, Android at some point introduced "unbundled" location providers. Apparently they needed that in order to move their own NetworkLocationProvider from the Android core into the Google Apps package.

    I haven't seen an unbundled location provider working in practice yet and I'm not aware of any open-source code using it, but it looks like recent version of Android have support for "aftermarket" location providers.

    Source code for the respective library is at
    https://github.com/CyanogenMod/android_frameworks_base/tree/cm-10.1/location/lib

    A location provider would have to extend com.android.location.provider.LocationProviderBase.

    Classes and methods are documented in the code.

    Edit: Looking at
    https://github.com/android/platform_frameworks_base/blob/master/core/res/AndroidManifest.xml
    starting at line 607, it looks like the protection level for the INSTALL_LOCATION_PROVIDER permission is signatureOrSystem. This means that any app which uses this feature (i.e. installs a location provider, not necessarily the locaion provider itself) would either need to be signed with the same key as the platform (which would require you to build your entire Android system from source) or be installed as a system application (which might work on rooted devices).

    There is some code at
    https://github.com/microg/NetworkLocation
    which seems to implement a LocationProvider. I haven't looked at it in detail (yet) or built it, so I cannot say if it works and what is requierd for it. But maybe it provides some pointers.

提交回复
热议问题