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.
You may try this way:
I tried this in Huawei Android 8, besides adding permission READ_PHONE_STATE in manifest, the phone needs to allow to "access phone ID" to the app. Then it works.
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)All you need to do is,
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.
That should work