Android的手机短信获取响应错误/通知,如果消息已经交付或发送成功 [英] Android sms getting the response error/notification if the message has been delivered or sent successfully

查看:234
本文介绍了Android的手机短信获取响应错误/通知,如果消息已经交付或发送成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 我怎么能发送消息和响应时,时已成功发送邮件得到的错误?

下面是我的code:

  {尝试
   字符串消息=世界,你好!现在我们将要展示+
      如何发送\\ n个消息比从Android程序\\ n 160个字符。
   SmsManager smsManager = SmsManager.getDefault();
   ArrayList的<串GT;部分= smsManager.divideMessage(消息);
   smsManager.sendMultipartTextMessage(phoneNumber的,零,部件,NULL,NULL);
}赶上(例外五){
   //我如何获得SMS阶跃响应HERE
   Toast.makeText(背景下,Toast.LENGTH_LONG短信faild!).show();
   e.printStackTrace();
}


解决方案

您不能发送或传递成功/失败,因为它不是一个例外,也没有任何其他类型的的Throwable

您需要创建的PendingIntent S代表中发送和接收操作,并通过他们在用于发送短信的方法。下面是我用来发送单一和多部分邮件的方式:

 公共静态最后弦乐ACTION_SMS_SENT =com.mycompany.myapp.SMS_SENT;
公共静态最后弦乐ACTION_SMS_DELIVERED =com.mycompany.myapp.SMS_DELIVERED;私人无效sendSMS(串号,弦乐味精)
{
    SmsManager SM = SmsManager.getDefault();
    ArrayList的<串GT;部分= sm.divideMessage(消息);    意图iSent =新意图(ACTION_SMS_SENT);
    的PendingIntent piSent = PendingIntent.getBroadcast(在此,0,iSent,0);
    意图IDEL =新意图(ACTION_SMS_DELIVERED);
    的PendingIntent piDel = PendingIntent.getBroadcast(在此,0,IDEL,0);    如果(parts.size()== 1)
    {
        弦乐味精= parts.get(0);
        sm.sendTextMessage(数字,空,味精,piSent,piDel);
    }
    其他
    {
        ArrayList的<&的PendingIntent GT; sentPis =新的ArrayList<&的PendingIntent GT;();
        ArrayList的<&的PendingIntent GT; delPis =新的ArrayList<&的PendingIntent GT;();        INT克拉= parts.size();
        的for(int i = 0; I< CT;我++)
        {
            sentPis.add(ⅰ,piSent);
            delPis.add(ⅰ,piDel);
        }        sm.sendMultipartTextMessage(数,零,部件,sentPis,delPis);
    }
}

然后,你将需要一个广播接收器

 公共类SmsReceiver扩展广播接收器
{
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图)
    {
        字符串行动= intent.getAction();        如果(action.equals(ACTION_SMS_SENT))
        {
            开关(的getResult code())
            {
                案例-1://Activity.RESULT_OK
                    打破;                案例SmsManager.RESULT_ERROR_NO_SERVICE:
                    打破;                案例SmsManager.RESULT_ERROR_NULL_PDU:
                    打破;                案例SmsManager.RESULT_ERROR_RADIO_OFF:
                    打破;                案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    打破;                默认:            }
        }
        否则,如果(action.equals(ACTION_SMS_DELIVERED))
        {
            开关(的getResult code())
            {
                案例-1://Activity.RESULT_OK
                    打破;                情况下0://Activity.RESULT_CANCELED
                    打破;                默认:
            }
        }
    }
}

情况块是pretty不言自明的,你想补充必要的,必要的情况下,code。

该接收器的实例可以与 registerReceiver()方法的活动进行注册,或者接收器类可以在清单中注册与<接收> <意向滤光器> 标签

How can i get the error when sending message and the response when the message has been successfully sent?

Here is my code:

try {    
   String message = "Hello World! Now we are going to demonstrate " + 
      "how to send a \n message with more than \n 160 characters from your Android application.";
   SmsManager smsManager = SmsManager.getDefault();
   ArrayList<String> parts = smsManager.divideMessage(message ); 
   smsManager.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
} catch (Exception e) {
   //HOW CAN I GET THE SMS RESPONES HERE
   Toast.makeText(context, "SMS faild!",Toast.LENGTH_LONG).show();
   e.printStackTrace();
}

解决方案

You cannot catch a Sent or Delivery success/failure, as it is not an Exception, nor any other type of Throwable.

You will need to create PendingIntents for Sent and Delivered Actions, and pass them in the method used to send the SMS. The following is the method I use to send both single and multi-part messages:

public static final String ACTION_SMS_SENT = "com.mycompany.myapp.SMS_SENT";
public static final String ACTION_SMS_DELIVERED = "com.mycompany.myapp.SMS_DELIVERED";

private void sendSMS(String number, String msg)
{
    SmsManager sm = SmsManager.getDefault();        
    ArrayList<String> parts = sm.divideMessage(message);

    Intent iSent = new Intent(ACTION_SMS_SENT);
    PendingIntent piSent = PendingIntent.getBroadcast(this, 0, iSent, 0);
    Intent iDel = new Intent(ACTION_SMS_DELIVERED);
    PendingIntent piDel = PendingIntent.getBroadcast(this, 0, iDel, 0);

    if (parts.size() == 1)
    {
        String msg = parts.get(0);
        sm.sendTextMessage(number, null, msg, piSent, piDel);
    }
    else
    {
        ArrayList<PendingIntent> sentPis = new ArrayList<PendingIntent>();      
        ArrayList<PendingIntent> delPis = new ArrayList<PendingIntent>();       

        int ct = parts.size();
        for (int i = 0; i < ct; i++)
        {
            sentPis.add(i, piSent);
            delPis.add(i, piDel);
        }

        sm.sendMultipartTextMessage(number, null, parts, sentPis, delPis);
    }
}

Then, you will need a BroadcastReceiver:

public class SmsReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();

        if (action.equals(ACTION_SMS_SENT))
        {
            switch (getResultCode())
            {
                case -1: //Activity.RESULT_OK
                    break;

                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    break;

                case SmsManager.RESULT_ERROR_NULL_PDU:
                    break;

                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    break;

                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    break;

                default:

            }
        }
        else if (action.equals(ACTION_SMS_DELIVERED))
        {
            switch (getResultCode())
            {
                case -1: //Activity.RESULT_OK
                    break;

                case 0: //Activity.RESULT_CANCELED
                    break;

                default:
            }
        }
    }
}

The case blocks are pretty self-explanatory, and you would add the code necessary in the cases needed.

An instance of this Receiver can be registered in an Activity with the registerReceiver() method, or the Receiver class can be registered in the manifest with <receiver> and <intent-filter> tags.

这篇关于Android的手机短信获取响应错误/通知,如果消息已经交付或发送成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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