0

I'm relatively new to Android programming. So far, I'm working on a messaging app that will send a message via SMS and have the option to append GPS coordinates to the message. The problem I have here is with SDK version 23, the permissions need to be requested at runtime, and I have not found a good example where I can request the needed permissions before running the app. The code sample I have is below.

If I comment out this code section, Android Studio compiles the code, but I get an application crash when I tap the button that sends the message. I do have the appropriate code in my AndroidManifest.xml for using SMS and GPS permissions.

if (ContextCompat.checkSelfPermission(MainActivity,
Manifest.permission.SEND_SMS)
    != PackageManager.PERMISSION_GRANTED)
{

// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity,
        Manifest.permission.SEND_SMS))
    {

        Toast.makeText(getBaseContext(),
                "Foo Messenger requires permission to send SMS",
                Toast.LENGTH_SHORT).show();

    }

    else
    {

    // No explanation needed, we can request the permission.

    ActivityCompat.requestPermissions(MainActivity,
            new String[]{Manifest.permission.SEND_SMS},
            MY_PERMISSIONS_REQUEST_SEND_SMS);

    }
}

if (ContextCompat.checkSelfPermission(MainActivity,
Manifest.permission.ACCESS_FINE_LOCATION)
    != PackageManager.PERMISSION_GRANTED)
{

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity,
            Manifest.permission.ACCESS_FINE_LOCATION))
    {

        Toast.makeText(getBaseContext(),
                "Foo Messenger requires permission to access GPS to be able to send your coordinates",
                Toast.LENGTH_SHORT).show();

    }

    else
    {

        // No explanation needed, we can request the permission.

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

      }
}
kgryczan
  • 1
  • 1
  • "I get an application crash when I tap the button that sends the message" -- [use LogCat to examine the Java stack trace associated with your crash](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this). You might also consider posting the code that is run when you tap the button, as I do not see an `OnClickListener` in what you have posted at the moment. – CommonsWare Oct 01 '15 at 19:19
  • My OnClickListener() only checks to see if the recipient ID and message fields both contain something, then calls another function to actually perform the send. I've read where any permission requests not granted by the user to apps that need access to features considered 'dangerous' can cause apps to crash, so this is why I only posted the snippet for granting the permissions. – kgryczan Oct 02 '15 at 14:29
  • This is the problem as identified in the LogCat '10-02 10:53:52.783 3726-3726/? E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: MyProcess, PID: 3726 java.lang.SecurityException: Sending SMS message: uid 10060 does not have android.permission.SEND_SMS.' – kgryczan Oct 02 '15 at 14:55
  • OK. Did you execute the code shown in your question? Did you accept the permission via the dialog that appeared when you made your `requestPermissions()` call? If you go into Settings > Apps > (your app) > Permissions, do you have the SMS permission? Is your `` element in the right place? – CommonsWare Oct 02 '15 at 15:00
  • I know the issue is with this snippet since I went into the settings for the app and manually granted the SMS and location permissions and the button does function as expected. – kgryczan Oct 02 '15 at 15:31
  • OK. Did you execute the code shown in your question? Did you accept the permission via the dialog that appeared when you made your `requestPermissions()` call? – CommonsWare Oct 02 '15 at 15:34
  • I can't execute the code since it does not compile within Android Studio. These are the errors I see: "can't resolve symbol checkSelfPermission", Manifest.permission.SEND_SMS and PackageManager.PermissionGranted are being recognized as unknown classes, and MainActivity being passed to Activity.Compat.requestPermissions() is not being seen as an expression. If I can get the permissions debugged here for SMS, it will also debug the other section with the GPS permissions. – kgryczan Oct 02 '15 at 15:51

1 Answers1

0

can't resolve symbol checkSelfPermission

You need to update to a v23 edition of the support-v4 artifact, in your dependencies in app/build.gradle (assuming that your app is in app/):

dependencies {
    compile 'com.android.support:support-v4:23.0.1'
}

Manifest.permission.SEND_SMS and PackageManager.PermissionGranted are being recognized as unknown classes

You need to add import statements for android.Manifest.permission and android.content.pm.PackageManager.

MainActivity being passed to Activity.Compat.requestPermissions() is not being seen as an expression

MainActivity is a class. Presumably, you want that parameter to be this, or maybe MainActivity.this.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491