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

时间:2014-08-06 16:13:17

标签: android api send samsung-mobile sendmessage

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

我还使用了Manifest中的权限

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

这是发送短信的活动

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);        
  }
}

0 个答案:

没有答案