Send sms permission doesn't work

31,537

Solution 1

All you need to do is,

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.roa.sendsms"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.SEND_SMS"/>

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name="com.example.homesafe.MainActivity"
            android:label="@string/app_name">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Try to run into the mobile instead checking it to the emulator. Just because emulator does not have the sim card as our mobiles have, so.

Solution 2

Try to use something like this. Not tested, but should work fine. Drop a note when you see some problem here.

This is just to show how permission granting in Android M works. Please extend it by functionalities mentioned on Android tutorial site about permissions.

You will need add ActivityCompat.shouldShowRequestPermissionRationale check to match best practices. I can extend this answer but I think it's not necessary. Just make sure you are granting permissions in runtime (or use targetSdkVersion lower then 23 - this is however not recommended)

private static final int PERMISSION_SEND_SMS = 123;

private void requestSmsPermission() {

    // check permission is given
    if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        // request permission (see result in onRequestPermissionsResult() method)
        ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.SEND_SMS},
            PERMISSION_SEND_SMS);
    } else {
        // permission already granted run sms send
        sendSms(phone, message);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {

            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permission was granted
                sendSms(phone, message);
            } else {
                // permission denied
            }
            return;
        }
    }
}

private void sendSms(String phoneNumber, String message){
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);
}

Solution 3

i had same problem you need to get runtime permission on setting>app>"your app" >enable sms permission

Share:
31,537
Codey
Author by

Codey

Updated on July 05, 2022

Comments

  • Codey
    Codey almost 2 years

    I want to send a simple message and i have <uses-permission android:name="android.permission.SEND_SMS"/> in my manifest, but I always get: java.lang.SecurityException: Sending SMS message: uid 10064 does not have android.permission.SEND_SMS.

    I checked this answer, but it still doesn't work.

    this is my manifest:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.roa.sendsms" >
    
    
    <uses-permission android:name="android.permission.SEND_SMS"/>
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    

    and this is my code:

    package com.roa.sendsms;
    
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.telephony.SmsManager;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
        Button sendButton =  (Button)findViewById(R.id.sendButton);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendSms(phoneNumber, "you get message from roish app!!");
            }
        });
    }
    
    private void sendSms(String phoneNumber, String message){
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }
    

    }

    thank to all.

    • VizGhar
      VizGhar over 8 years
      I guess you are testing it on Android M (6) am I right?
    • VizGhar
      VizGhar over 8 years
      what you mean by uses-sdk? He has it specified in build.gradle file probably
    • VizGhar
      VizGhar over 8 years
      If you are testing on Android M, install app on device and check application permission in settings. Try to turn permission on if its there. If it will work now, follow instructions at developer.android.com/training/permissions/requesting.html
    • IntelliJ Amiya
      IntelliJ Amiya over 8 years
      @VizGhar Got it . Your link useful
    • Codey
      Codey over 8 years
      I run it on the emulator. I will try to install it on my device...
    • Codey
      Codey over 8 years
      in my device it show the premission but not sending the message
    • Codey
      Codey over 8 years
      ok. it so weird, in the emulator the app crashed. but in my device it work. why?
    • VizGhar
      VizGhar over 8 years
      Since android Marshmallow release there are many changes in permissions API. For devices with Marshmallow and up, permissions are handled different way. On your device you have probalby earlier version of android
    • Christian
      Christian over 8 years
      Thank you very much VizGhar, I had exactly this problem on Android 6, you helped me to solve it ! Thanks again...
  • Codey
    Codey over 8 years
    it so weird, in the emulator the app crashed. but in my device it work. why?
  • Parth Bhayani
    Parth Bhayani over 8 years
    Coz the emulator does bot have the sim card as our mobiles have, so that is the reason why your app crashed.
  • Parth Bhayani
    Parth Bhayani over 8 years
    Thats why i told you that it works for me, why it doesn't for you. Anyways njoy and hv fun :)
  • Parth Bhayani
    Parth Bhayani over 8 years
    If this answer helpful to you, you can accept this answer and vote it as well.
  • VizGhar
    VizGhar over 8 years
    This will work for eclipse (please don't use it) or if some configuration is missing from build.gradle. if you have configured android.defaultConfig.targetSdkVersion in build.gradle; targetSdkVersion from manifest won't be used.
  • VizGhar
    VizGhar over 8 years
    however you can make it work by setting targetSdkVersion in gradle to lower then 23, this is not recommended. Try to allways target most recent SDK
  • Codey
    Codey over 8 years
    thanks guy. right now my code is working. as you can see in my answer here, the problem was in the emulator. but maybe i will try it later. thank a lot