【问题标题】:SMS Sending issues in android kitkatandroid kitkat 中的短信发送问题
【发布时间】:2014-12-30 12:58:40
【问题描述】:

我在三星设备中发送短信时经常遇到这个问题。

安卓版本 Android 4.0.3 - 4.0.4

这是我收到的错误报告

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at android.os.Parcel.readException(Parcel.java:1333)
at android.os.Parcel.readException(Parcel.java:1281)
at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:644)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:149)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:99)
at com.msh7.utilities.SMSSender$SMSProgressTask.doInBackground(SMSSender.java:87)
at com.msh7.utilities.SMSSender$SMSProgressTask.doInBackground(SMSSender.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more

在第 87 行,我有这段代码

sms.sendTextMessage(destinationNumber, null, msg, sentPI, deliveredPI);

所有这些方法都在AsyncTaskdoInBackground()方法中。

有人可以帮我吗?

*注意:我的应用在后台发送短信,它不是默认短信应用。

【问题讨论】:

标签: android smsmanager


【解决方案1】:

我猜这是由于文本的大小限制。最大允许字符数为 159。因此您必须将消息分成几部分并发送。

【讨论】:

    【解决方案2】:

    //尝试使用此服务发送短消息或长消息

    import java.util.ArrayList;
    import java.util.Timer;
    import java.util.TimerTask;
    
    
    import android.app.Activity;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.BroadcastReceiver;
    import android.content.ContentValues;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.net.Uri;
    import android.os.IBinder;
    import android.telephony.gsm.SmsManager;
    
    
    public class SendMsgService extends Service{
        String SENT = "SMS_SENT";
        String DELIVERED="SMS_DELIVERED";
    
    
    
        public void sendShortOrLongmsg(String PhoneNo,String txt){
             if(PhoneNo.trim().length() == 0) { 
    //            //Toast.makeText(getApplicationContext(), "Please enter a Phone Number.", Toast.LENGTH_LONG).show();
                  return;
              }
    
              if(txt.trim().length() == 0) { 
                  //Toast.makeText(getApplicationContext(), "Please enter your message.", Toast.LENGTH_LONG).show();
                  return;
              }
    
              if(txt.trim().length() > 160) {
                  sendLongSMS(PhoneNo,txt); 
    
    
    
    
              }
              else {
                  sendSMS(PhoneNo,txt);
                 }
              Timer t=new Timer();
              t.schedule(new RemindTask(), 20000);
        }
    ///////////////////////////////////////////////
    
    
    
        public static void sendSMS(final String phoneNo,final String txt) {
            //String phoneNo = "0123456789";
            //String message = "Hello World!";
            PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(SENT), 0);
            PendingIntent deliveredPI = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(DELIVERED), 0);
    
            //---when the SMS has been sent---
            getApplicationContext().registerReceiver(new BroadcastReceiver(){
    
            @Override
            public void onReceive(Context context, Intent intent) {
    
                switch (getResultCode())
            {
            case Activity.RESULT_OK:
            //Toast.makeText(getApplicationContext(), "SMS sent", Toast.LENGTH_SHORT).show();
    
    
    
            break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
            //Toast.makeText(getApplicationContext(), "Generic failure", Toast.LENGTH_SHORT).show();
    
    
    
            break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
            //Toast.makeText(getApplicationContext(), "No service", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
            //Toast.makeText(getApplicationContext(), "Null PDU",       Toast.LENGTH_SHORT).show();
            break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
            //Toast.makeText(getApplicationContext(), "Radio off", Toast.LENGTH_SHORT).show();
            break;
            }
    
            }
            }, new IntentFilter(SENT));
    
            //---when the SMS has been delivered---
            getApplicationContext().registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {               
            switch (getResultCode())
            {
            case Activity.RESULT_OK:
            //Toast.makeText(getApplicationContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
            break;
            case Activity.RESULT_CANCELED:
            //Toast.makeText(getApplicationContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
            break;                        
            }
            }
            }, new IntentFilter(DELIVERED));        
    
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNo, null, txt, sentPI, deliveredPI); 
           }
    
        ////////////////////////////////////////////////
        /////////////////////////////////////////////////
        public void sendLongSMS(final String PhoneNo,final String txt) {         
    
            SmsManager smsManager = SmsManager.getDefault();
            ArrayList<String> parts = smsManager.divideMessage(txt); 
    
            int messageCount = parts.size();
    
            ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
            ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
    
            PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(SENT), 0);
            PendingIntent deliveredPI = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(DELIVERED), 0);
    
            for (int j = 0; j < messageCount; j++) {
                sentIntents.add(sentPI);
                deliveryIntents.add(deliveredPI);
            }
    
            //---when the SMS has been sent---
            getApplicationContext().registerReceiver(new BroadcastReceiver(){
    
    
            public void onReceive(Context context, Intent intent) {
    
                switch (getResultCode())
            {
            case Activity.RESULT_OK:
            //Toast.makeText(getApplicationContext(), "SMS sent",       Toast.LENGTH_SHORT).show();
    
    
    
    
    
            break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
            //Toast.makeText(getApplicationContext(), "Generic failure", Toast.LENGTH_SHORT).show();
    
    
            break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
            //Toast.makeText(getApplicationContext(), "No service", Toast.LENGTH_SHORT).show();
    
             //getContentResolver().insert(Uri.parse("content://sms/failed"), values);
            break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
            //Toast.makeText(getApplicationContext(), "Null PDU",       Toast.LENGTH_SHORT).show();
            break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
            //Toast.makeText(getApplicationContext(), "Radio off", Toast.LENGTH_SHORT).show();
            break;
            }
    
            }
            }, new IntentFilter(SENT));
    
            //---when the SMS has been delivered---
            getApplicationContext().registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {               
            switch (getResultCode())
            {
            case Activity.RESULT_OK:
            //Toast.makeText(getApplicationContext(), "SMS delivered",      Toast.LENGTH_SHORT).show();
            break;
            case Activity.RESULT_CANCELED:
            //Toast.makeText(getApplicationContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
            break;                        
            }
            }
            }, new IntentFilter(DELIVERED));        
    
    
    
            smsManager.sendMultipartTextMessage(PhoneNo, null, parts, sentIntents,deliveryIntents);
    
         }
    

    【讨论】:

      【解决方案3】:

      老兄,你得到 NullPointerException。好像您还没有初始化 sms 对象。 请检查..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-04
        相关资源
        最近更新 更多