Search code examples
androidservicelocationlistener

run location listener in background android 6


I have a project that should have an Location based Alarm. But my problem is that I use Android 6 and when I want to use Location Listener Service , must ask permission from the user and it need to have an activity or context.But, I don't have my first question is to tell me , how can I use the Location Listener Services? and second,I want to say about permission problem. I have tow activity, first is launcher and user can add Alarm in this activity,and second Activity has google map and user can add marker to save alarm when user save alarm and don't close application no problem when user near to the marker location alarm is show,but when user close the app,Location Listener not work

second Activity enter image description here

first Activity enter image description here


Solution

  • The easiest way is to request permissions from whatever activity you do have. Then from the service you can just check if you have them. There is no way to request permissions from the background currently.

    What you can try (I haven't yet)- the code for request permissions just does this:

        Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
        startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
    

    buildRequestPermissionsIntent is not publicly available. But you could try calling it using reflection, or by redefining the PackageManager class so it has visible access.