Today I met error on android send SMS function with below error message
"does not have android.permission.SEND_SMS"
This problem came up on android API 23.
I never met this kind of error under API 23.
What I found solution is that
first, give SEMD_SMS permission on AndroidManifest.xml file
<uses-permission android:name="android.permission.SEND_SMS" />
then, I implemented code like this
// first, call address intent to get person mobile number public void sendBySMS() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setData(ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, REQ_CODE_SMS_ADDRESS);
}
// second, after get person mobile number, call send sms function
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent data) {
switch (requestCode) {
case REQ_CODE_SMS_ADDRESS:
if (responseCode == RESULT_OK) {
Cursor cursor = getContentResolver().query(data.getData(),
new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER}, null, null, null);
cursor.moveToFirst();
mName = cursor.getString(0);
mMobileNumber = cursor.getString(1);
cursor.close();
// call send sms function which is created by me.
smsMessageSent();
}
break;
}}
// call intent to send sms
public void smsMessageSent() {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse( "sms:" + mMobileNumber ) );
intent.putExtra( "sms_body", mContentToSend );
startActivity(intent);
}
Here is what i used before (I mean under API 23 version)
It's just reference
public void smsMessageSent() {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver() {
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(mMobileNumber, null, mContentToSend, sentPI, deliveredPI);
}
최근댓글