在Android中以编程方式发送短信

在Android中以编程方式发送短信,android,api,send,samsung-mobile,sendmessage,Android,Api,Send,Samsung Mobile,Sendmessage,我正在尝试使用以下导入[导入android.telephony.smsmsmanager;]。。。。我第一次运行代码时,使用我的——三星Galaxy Note 3——,它运行得很好。然后我尝试发送另一条消息,但它不起作用,并且它一直显示一条{Generic failure}消息。然后我尝试正常地从我的Android消息应用程序发送消息,但它显示了一条[发送短信失败]的消息 我还使用了清单中的权限 <uses-permission android:name="android.permiss

我正在尝试使用以下导入[导入android.telephony.smsmsmanager;]。。。。我第一次运行代码时,使用我的——三星Galaxy Note 3——,它运行得很好。然后我尝试发送另一条消息,但它不起作用,并且它一直显示一条{Generic failure}消息。然后我尝试正常地从我的Android消息应用程序发送消息,但它显示了一条[发送短信失败]的消息

我还使用了清单中的权限

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

这是由于结果错误通用故障,sentPI可能包括额外的“错误代码”,包含无线电技术特定值,通常仅用于故障排除。检查特定的错误代码以进行更多调试。它不会在LogCat中显示任何内容!!!我看不到任何错误的迹象,我的意思是一个额外的错误代码,您必须添加到您的代码中。如果(i.hasExtra(“errorCode”){return i.getStringExtra(“errorCode”);}否则{return“未知错误。没有“errorCode”字段。”;}我不确定是否还有其他代码需要添加?!?!?!因为就我而言,这在狂饮时非常有效,然后它就停止工作了?!?Ankit给了你密码。除此之外,如果股票消息应用程序也出现了故障,那么听起来像是设备和/或帐户问题。尝试重新启动设备。此外,如果您在测试时发送了相当多的消息,则可能已达到每小时的限制。您可以稍等片刻,然后再试一次。
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsManager;

import android.widget.TextView;
import android.widget.Toast;
public class SMS_contacts_Activity extends Activity {
public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);


       String Message="Hi there this is a message from my app";
       String Number ="55568103";

       sendSMS(Number, Message);
}

 //---sends an SMS message to another device---
public void sendSMS(String phoneNumber, String message)
{        
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
        new Intent(SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
        new Intent(DELIVERED), 0);
    //---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent ", 
                            Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure ", 
                            Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service ", 
                            Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU ", 
                            Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off ", 
                            Toast.LENGTH_LONG).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered", 
                            Toast.LENGTH_LONG).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", 
                            Toast.LENGTH_LONG).show();
                    break;                        
            }
        }
    }, new IntentFilter(DELIVERED));        

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
  }
}