0

In using the method below, Android Studio is generating the following error on the keyword "this":

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_READ_FINE_LOCATION);

Error:

Wrong first argument type...found com.websmithing.wp.gpstracker.LocationService, required android.app. Activity

I can reference the activity using "this" without any error when evaluating the manifest file permissions check as per below.

ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)   !=
  PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) !=
  PackageManager.PERMISSION_GRANTED) {

Can anyone point me in the right direction?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
leslieG
  • 1
  • 1

3 Answers3

0

this refers to the present class. It only works with this when you are in an activity while requesting permission.

Solution

Send a notification from the service so when the user clicks it, open the activity and ask for the permissions you need.

How do I send a notification?

Simple refer to Building a Notification.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jagapathi
  • 1,635
  • 1
  • 19
  • 32
0

Pay attention to the error message. It's pointing you toward the solution.

Wrong first argument type...found com.websmithing.wp.gpstracker.LocationService, required android.app. Activity

It means that ActivityCompat.requestPermissions() is expecting an Activity as the first argument but you are providing a LocationService instance. I guess you are invoking this from an inner class, then perhaps you have to use WhateverYouActivityClassIs.this to refer to the outer class.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • Yes, I am invoking from an inner class. Guess I just don't understand why the reference "this" works for the checkSelfPermissions as a valid activty but not for the request code... All the examples I have researched online use "this" as a valid form.... – leslieG May 22 '17 at 22:39
  • I believe my issue is that the inner class from which I am calling request permissions extends the Service class and is not an activity. Therefore the reference mainActivity.this will not work either. – leslieG May 31 '17 at 16:06
-1

You have to write the checkSelfpermission code in the activity where you call the class.

Jérémie Bertrand
  • 3,025
  • 3
  • 44
  • 53
Ajmal Tk
  • 153
  • 1
  • 4
  • Could you explain your answer and give us an example? – Dima Kozhevin Oct 25 '17 at 11:52
  • While this might be a valuable hint to solve the problem, a good answer also demonstrates the solution. Please [edit] to provide example code to show what you mean. Alternatively, consider writing this as a comment instead. – Toby Speight Oct 25 '17 at 12:34