如何通过编程方式在android中发送短信? [英] How to send sms in android programmatically ?

查看:110
本文介绍了如何通过编程方式在android中发送短信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式在android中发送短信?

i想要创建一个按钮,当我点击时会发送我存储在字符串中的短信。

解决方案

 private 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 deliverPI = PendingIntent.getBroadcast(this,0,
new Intent(DELIVERED),0);

// ---当发送短信时---
registerReceiver(新的BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1){
switch(getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(),SMS sent,
Toast .LENGTH_SHORT)。show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(),Generic failure,
Toast.LENGTH_SHORT).show ();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(),No service,
Toast.LENGTH_SHORT).show();
休息;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(),Null PDU,
Toast.LENGTH_SHORT)。show();
休息;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(),Radio off,
Toast.LENGTH_SHORT).show();
休息;
}
}
},新的IntentFilter(SENT));

// ---当SMS已经发送时---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1){
switch(getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(),SMS deliver,
Toast .LENGTH_SHORT)。show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(),SMS未送达,
Toast.LENGTH_SHORT)。 show();
break;
}
}
},新的IntentFilter(DELIVERED));

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



或者:

http://katharnavas.wordpress.com/2009/10/07/how-to-send-sms-from-android-programatically/ [ ^ ]


How to send sms in android programmatically?
i wanna create a button which when i will click will send my sms which have stored in a string.

解决方案

private 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_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).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_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;                        
                }
            }
        }, new IntentFilter(DELIVERED));        

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


or else :
http://katharnavas.wordpress.com/2009/10/07/how-to-send-sms-from-android-programatically/[^]


这篇关于如何通过编程方式在android中发送短信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆