I'm developing an android application that sends text messages. The shipping code is this
Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.BIND_CARRIER_SERVICES" />
<uses-permission-sdk-23 android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission-sdk-23 android:name="android.permission.RECEIVE_SMS"/>
<uses-permission-sdk-23 android:name="android.permission.SEND_SMS"/>
<uses-permission-sdk-23 android:name="android.permission.INTERNET" />
<uses-permission-sdk-23 android:name="android.permission.BIND_CARRIER_SERVICES" />
<uses-permission-sdk-23 android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Code:
SmsManager smsManager
= SmsManager.getDefault();
smsManager.sendTextMessage( sms.getNumber(), providerNumber,
sms.getContent(), sentPI, deliveredPI);
But when the app works the message remains in default SMS app as not sent. I' ve to enter to default app and touch one by one and it sends.
This is my build.gradle
apply plugin: 'com.android.application'
repositories {
jcenter()
}
android {
compileSdkVersion 23
buildToolsVersion '25.0.3'
defaultConfig {
applicationId "xxxxxx.xxxxxxx"
minSdkVersion 22
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:23.4.0'
testCompile 'junit:junit:4.12'
compile 'com.github.kittinunf.fuel:fuel:1.4.0' //for JVM
compile 'com.github.kittinunf.fuel:fuel-android:1.4.0' //for Android
compile 'com.github.kittinunf.fuel:fuel-rxjava:1.4.0' //for RxJava support
}
How can I solve this?
To send the messages using the default provider you need to use "null" as the provider argument Like this
smsManager.sendTextMessage( sms.getNumber(), null, sms.getContent(), sentPI, deliveredPI);
I am using this
smsManager.sendTextMessage(number, null, message, null, null);
in one of my app and this works perfectly.
Note: 23+ app need to have the element(s) in manifest, but you also have to ask for those permissions at runtime from the user on Android 6.0+ devices, using methods like
checkSelfPermission()
and
requestPermissions()
Finally I found the solution... it was that I thought. Trouble is that I was trying to send SMS from another Thread. It's assumed to send from Main Ui Thread. The code provided by Google works only in Main UI Thread. Currently I'm trying to use a service to see if works!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.