无法使用我编写的代码在android上发送短信

问题描述 投票:-1回答:3

我正在尝试构建我的第一个应用程序,它将发送一个SMS消息。一切看起来都不错:1. SMS应用程序打开2.插入URI3.输入文字信息

仅不执行最后一次按下“ SEND”按钮。

我正在使用演示的代码here on youtube

包括此AndroidManifest.XML

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="il.ac.ruppin.reco.www.sendsmsyoutube">

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

    <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>

</manifest>

和此MainActivity.java

package il.ac.ruppin.reco.www.sendsmsyoutube;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

        Uri uri = Uri.parse("smsto:+972528524520");
        Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
        intent.putExtra("sms_body","Message from my new application");
        startActivity(intent);
    }
}

感谢您的帮助

java android sms
3个回答
1
投票

SMS的正确URI格式为sms:(和not smsto:

String number = "+972528524520"
Uri uri = Uri.parse("sms:" + number);

Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "Message from my new application");
startActivity(intent);

这将启动发送SMS消息的默认活动。


0
投票

您可以使用SmsManager类尝试它

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null)

其中,phoneNo =发送给,sms =您要传递的消息


0
投票

根据Hardik Vegad的回答,我使用了SmsManager。我要做的就是允许应用程序发送短信。设置>应用程序> MyAp>权限

感谢您的帮助

© www.soinside.com 2019 - 2024. All rights reserved.