4

I figured that in M requestPermissions() can only be called by an Activity class. I have an app that has no UI screen. It runs as a background service. Does this mean that I have to put a spinner screen if checkSelfPermissions returns denied?

cuihtlauac
  • 1,808
  • 2
  • 20
  • 39
Sai
  • 2,089
  • 3
  • 19
  • 30

1 Answers1

1

I have an app that has no UI screen.

Then it will never run, unless it is preinstalled on an Android device or custom ROM, or it is a plugin to some other app. Otherwise, your app will remain in the stopped state forever. Pretty much every app distributed through normal channels, including the Play Store, needs an activity.

Does this mean that I have to put a spinner screen if checkSelfPermissions returns denied?

I do not know what "a spinner screen" is. AFAIK, the recommended pattern for a service needing a runtime permission that it does not have is to:

  1. Raise a Notification, to let the user know that the service cannot do its work without this permission.

  2. Have the Notification trigger an Activity that can call requestPermissions(). Optionally, this activity can have Theme.Translucent.NoTitleBar, so the only visible UI is the permission dialog.

  3. If onRequestPermissionResult() indicates that you have the permission, the activity can tell the service to go ahead (e.g., via a call to startService()), then finish() itself. If onRequestPermissionResult() indicates that the user denied the permission, do whatever makes sense (e.g., show the Notification again, gracefully shut down, suggest to the user that the user uninstall the app).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Also, you must be aware that the user can turn off some permissions for the the app whenever he wants from the settings page. – Beemo Jan 15 '16 at 19:04
  • 2
    Sorry for the confusion, I am an OEM developer working on a pre-installed system app. The APK has no UI/Activity, it provides AIDL to other apps. – Sai Jan 20 '16 at 19:20