简体   繁体   English

没有用户互动,Android不会发送短信

[英]Android doesn't send SMS message without user interaction

I'm developing an android application that sends text messages. 我正在开发一个发送短信的android应用程序。 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. 但是,当应用程序运行时,消息仍保留在默认的SMS应用程序中,未发送。 I' ve to enter to default app and touch one by one and it sends. 我必须进入默认应用,然后一一触摸,然后发送。

This is my build.gradle 这是我的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 要使用默认提供程序发送消息,您需要使用“ null”作为提供程序参数

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 注意:23+应用程序需要在清单中包含元素,但是您还必须在运行时向Android 6.0+设备上的用户要求使用这些方法的权限,例如

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. 问题是我试图从另一个线程发送SMS。 It's assumed to send from Main Ui Thread. 假定是从主Ui线程发送的。 The code provided by Google works only in Main UI Thread. Google提供的代码仅在主UI线程中有效。 Currently I'm trying to use a service to see if works! 目前,我正在尝试使用一项服务,以查看是否有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM