java - Oreo 8.0 - 'sendTextMessage()' 未将邮件保存到已发送文件夹

标签 java android

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

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

list :

<?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'
}

主要 Activity :

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 文档,这是注定要发生的。

Note: Beginning with Android 4.4 (API level 19), if and only if an app is not selected as the default SMS app, the system automatically writes messages sent using this method to the SMS Provider (the default SMS app is always responsible for writing its sent messages to the SMS Provider). For information about how to behave as the default SMS app, see Telephony.

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

我在这里遗漏了什么吗?谁能帮忙? :)

最佳答案

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

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

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

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

对于有类似问题的人,可能只弹出 SMS Activity 某些带有一些古怪代码的模型.. 或者通过尝试保存虚拟短信并查看它是否有效来检测是否存在此问题(记住将其删除如果它确实有效).. 如果失败则显示 SMS Activity ......

IMO 的每一个变通办法都是令人不安的困惑,我愿意接受任何人提出的任何建议。 :)

关于java - Oreo 8.0 - 'sendTextMessage()' 未将邮件保存到已发送文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51537810/

相关文章:

java - 换肤 Java 桌面应用程序?

android - RecyclerView 和 GridLayoutManager : making one cell bigger

java - 如何从 Android 调用 javascript?

java - Android致命异常错误

Android如何从服务中的特定类获取上下文

Java:将项目添加到 ArrayList<ArrayList<Integer>> 时遇到问题

java - HttpURLConnection 上的 Tomcat 内存泄漏警告

java - 为什么这个 while 循环不结束?

java - 是否有标准的 Java Receiver/Handler 接口(interface)?

android - Google 登录错误状态 {statusCode=DEVELOPER_ERROR, resolution=null}