2

I am aware about the changes introduced in Android 6.0/SDKVersion 23 regarding the run-time permission. There is already discussion and talks on this topic in the below post which talks about the various aspects of new permission model.

"How to request permissions from a Service in Android Marshmallow"

Google I/O 2015 - Android M Permissions

"Mother, May I?" Asking for Permissions (Android Dev Summit 2015)

After going thorough these article,I believe below is suggested to "workaround" (as Service doesnot have UI support)this problem.

  1. Check permission checkSelfPermission() in the context of Service.
  2. Notify it to status bar in case the permission is denied.
  3. User would now click on status bar which would launch a new DialogActivity to request for permission when users press on the notification.

I am not able to make the workable version of the suggestions provided to achieve this task. When we launch DialogActivity to ask for permission, it would be assigned and available for that activity. It would not be applicable for background service who had put it on status bar and even after this service would not be granted this permission(instead the permission would be given to DialogActivity).

Could somebody provide the input(possibly workable logic/code) so that I can make it workable(.i.e. execute the code in the context of Service which would be dependent on whether permission is granted or not).

My Scenario

Develop application which would send the SMS at regular interval regarding the my current location.

I have designed the UI for this application as mentioned below. It had some settings parameters and also buttons which would start the main logic which is:

  1. Start monitoring the current location and

  2. Send the SMS to configured contact numbers.

enter image description here

I believe user would minimize this application( as it does not have any interesting logic apart from sending the SMS) and it should continue to work in background(.i.e. monitoring the location and sending the SMS). Hence I thought to use move this logic in Service instead of keeping it the Activity itself. If I keep these logic in Activity itself, it works as expected however for that user is suppose to keep the application in foreground all the time.

So far I have been able to achieve the following:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Define START/STOP button handles.
        mStartApp = (ImageButton)findViewById(R.id.startApp);
        mStopApp = (ImageButton)findViewById(R.id.stopApp);

        //Write event handlers for above grabbed buttons.
        mStartApp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent startService = new Intent(getApplicationContext(),
                CurrentLocationTrackerService.class);
                startService(startService);
            }
        });

        mStopApp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent stopService = new Intent(getApplicationContext(),
                        CurrentLocationTrackerService.class);
                stopService(stopService);
            }
        });

        // Support for the ActionBar widgets menu in current activity.
        Toolbar mToolBar = (Toolbar)findViewById(R.id.myToolbar);
        if(mToolBar != null) {
            setSupportActionBar(mToolBar);
        }
    }

        //Other methods.....
}




public class CurrentLocationTrackerService extends Service {
    public CurrentLocationTrackerService() { }
    @Override
    public void onCreate() { }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int permissionLocationAccess = ContextCompat.checkSelfPermission(getApplicationContext(),
                Manifest.permission.ACCESS_FINE_LOCATION);
        // If permissionLocationAccess is true execute further 
        // If permissionLocationAccess false(which would always as we are in Service and it is dangerous
        // permission) we need to put some information to status bar. Post that user would click on that
        // which would launch some new activity where UI of asking permission would be shown. But still
        // Service would not get the permission( only new activity would have that permission but it is
        // of no use here as I am planning to put these logic iside the Service as my app may not be always
        // be in foreground)
    }



    }

Hope I have provided all detail regarding the problem and also my application context regarding why I require to model in this way.The real point over here is how to achieve this.

I am really stuck at this point and any help would be highly appreciated. Kindly let me know in case anything else is required from my side.

Community
  • 1
  • 1
Mantosh Kumar
  • 5,659
  • 3
  • 24
  • 48
  • 1
    Why not just request the permission inside your Activity? Permissions are persistent across app restarts, once the user accepts the permission they have to manually revoke it. – Andrew Sun Sep 09 '16 at 12:58
  • Hello do you want example code to save, ask, store permissions ? – Rodriquez Sep 09 '16 at 13:01
  • 2
    Runtime permissions are granted on a per-app basis. There is no concept in Android of an activity having a runtime permission and a service from the same app *not* having that runtime permission. – CommonsWare Sep 09 '16 at 13:07
  • 1
    *When we launch DialogActivity to ask for permission, it would be assigned and available for that activity. It would not be applicable for background service* that is incorrect. CommonsWare explained why – Tim Sep 09 '16 at 13:15
  • @CommonsWare: Thank you and all very much for explaining this concept regarding the runtime permissions are for per app basis and not per component within app. – Mantosh Kumar Sep 14 '16 at 04:36

0 Answers0