【问题标题】:Oreo 8.0 - 'sendTextMessage()' is not saving the message to the sent folderOreo 8.0 - 'sendTextMessage()' 未将消息保存到已发送文件夹
【发布时间】:2020-08-29 13:57:29
【问题描述】:

我正在尝试使用“sendTextMessage”或“sendMultipartTextMessage”从我自己的应用程序发送短信。对于高于 API 19 (KitKat) 的手机,此消息将保存到发送的文件夹中。但是在我的 Android 8.0 Oreo 手机上,它不会保存到已发送的项目中。

我创建了一个非常基本的测试应用程序,以便在此处发布。当 MainActivity 的 Resume 函数触发时,此 App 将简单地检查权限并发送文本。这是代码。

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.focus4software.www.myapplicationtest">

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

构建.等级

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.focus4software.www.myapplicationtest2"
        minSdkVersion 14
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

主活动:

package com.focus4software.www.myapplicationtest;

import android.Manifest;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Telephony;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_RESULTCODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onResume(){
        super.onResume();


        //Check Permissions first
        if (android.os.Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
                //Permissions not found..  request them
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}, REQUEST_RESULTCODE);
            }
            else {
                this.SendSMS();
            }
        }
        else {
            this.SendSMS();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case REQUEST_RESULTCODE: {
                if (grantResults.length == 1) {
                    //Make sure none of the permissions were denied
                    boolean somethingDenied = false;
                    for (int result : grantResults){
                        if (result != PackageManager.PERMISSION_GRANTED){
                            somethingDenied = true;
                        }
                    }

                    if (somethingDenied){
                        //a permission was denied
                        Toast.makeText(getApplicationContext(), "Failed to Send The TEST SMS, Permission was denied", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        //turn the app on.. permissions accepted
                        this.SendSMS();
                    }
                }
                else {
                    Toast.makeText(getApplicationContext(), "Failed to Send The TEST SMS, incorrect amount of permissions returned.", Toast.LENGTH_SHORT).show();
                }
                return;
            }
        }
    }

    private void SendSMS (){
        String phone = "[INSERT PHONE NUMBER]";
        String message = "InCodeTestExtra";

        //send sms
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phone, null, message, null, null);

        //Show we got here
        Toast.makeText(getApplicationContext(), "Code Executed...  SMS Passed on.", Toast.LENGTH_SHORT).show();

        //Save SMS
        //this.SaveSMS(getApplicationContext(), phone, message);
    }

    private void SaveSMS(Context inContext, String inAddress, String inBody) {
        try {
            ContentValues values = new ContentValues();
            values.put("address", inAddress);
            values.put("body", inBody);
            values.put("read", 1);
            values.put("date", System.currentTimeMillis());
            //values.put("status", delivered);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                Uri uri = Telephony.Sms.Sent.CONTENT_URI;
                inContext.getApplicationContext().getContentResolver().insert(uri, values);
            }
            else {
                inContext.getApplicationContext().getContentResolver().insert(Uri.parse("content://sms/sent"), values);
            }

            //notify of the save
            Toast.makeText(getApplicationContext(), "SMS SAVED (Maybe)", Toast.LENGTH_SHORT).show();
        } catch (Exception ex) {
            //notify of the Failure
            Toast.makeText(getApplicationContext(), "SMS Failed to save (" + ex.getMessage() + ")", Toast.LENGTH_SHORT).show();
        }
    }
}

如上所述,这不会将消息保存到我的 Android Oreo 手机的发送文件夹中。

根据 Android 文档,这是注定要发生的。

注意:从 Android 4.4(API 级别 19)开始,当且仅当应用 未选为默认短信应用,系统自动 将使用此方法发送的消息写入 SMS 提供程序( 默认 SMS 应用程序始终负责将其发送的消息写入 SMS 提供商)。有关如何作为默认行为的信息 短信应用,请参阅电话。

作为一种解决方法,我尝试手动保存消息。在 SendSMS 函数的底部,这被注释掉了。但是,运行此代码不会导致异常,但也不会将 SMS 保存到发送文件夹。这也适用于旧手机。我不确定这是否是相关问题。

我在这里有什么遗漏吗?任何人都可以帮忙吗? :)

【问题讨论】:

  • 消息发送成功了吗?也就是说,收件人真的得到了吗?您正在测试的手机的品牌/型号是什么?
  • 是的,消息发送正常。它只是不在发送的文件夹中。我正在使用华为荣耀 9 作为测试。
  • 好吧,我不得不说这个特定问题是特定于该模型的,因为我没有听说过任何其他模型的确切行为。 8.0 中存在一个错误,如果应用程序也没有拥有READ_PHONE_STATE 权限,则会抛出SecurityException,但我认为这不是一种表现,因为该异常应该会破坏整个发送,并且没有沉默。据报道,我听说的唯一另一个模糊相似的问题是通过在该方法中传递PendingIntents 的发送和交付解决的,但那是一个自定义 ROM。远射,但你可以试一试。
  • 我会在另一个模型上尝试一下。当我有时,将不得不在这里追捕一个并在这里更新。关于 READ_PHONE_STATE 和使用 Pending Intents,这个问题来自一个更大的应用程序,该应用程序使用“sendMultipartTextMessage”,同时拥有该权限并传入意图,问题仍然存在。我将继续尝试其他安装了 android 8.0 的设备,看看会发生什么。我也会在一些模拟器上试试这个,看看它是否在那里发生。有的时候会在这里更新,谢谢你的提示:):)
  • 我和华为荣耀也有同样的问题...

标签: java android


【解决方案1】:

只是想将这个线程更新为我发现的内容。

看起来 Mike M 是正确的,因为这是特定于某些型号的。我使用的是荣耀 9,另一个堆栈溢出用户在 p20 lite 上遇到了类似的问题。都是华为。我对许多不是华为的模型进行了许多测试,但从未设法复制该问题。

正如我的问题中提到的,如果短信不存在,使用代码手动保存短信也无法保存短信……也许这适用于这种情况下的其他用户。

我发现唯一可行的解​​决方法是显示短信活动并让用户自己发送短信。缺点是它需要用户输入,您只需填充 SMS 活动让他们发送消息。 这是一个关于如何做到这一点的有用链接: launch sms application with an intent

对于有类似问题的人,可以只使用一些时髦的代码弹出 SMS Activity 某些型号.. 或者通过尝试保存虚拟短信并查看它是否有效来检测是否存在此问题(记得删除它如果它确实有效).. 如果它失败了,则显示 SMS 活动......

IMO 每种解决方法都非常混乱,我愿意接受任何人的任何建议。 :)

【讨论】:

    猜你喜欢
    • 2020-01-30
    • 2012-04-28
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 2016-10-02
    相关资源
    最近更新 更多