Проблемы с отправкой SMS в android kitkat

Я получаю эту проблему большую часть времени на устройствах Samsung при отправке смс.

Версия Android 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);

и весь этот метод находится внутри метода AsyncTask doInBackground().

Может ли кто-нибудь помочь мне в этом?

*Примечание. Мое приложение отправляет смс в фоновом режиме и не является приложением для смс по умолчанию.


person Michael Shrestha    schedule 30.12.2014    source источник
comment
Дублируйте stackoverflow.com/questions/7832864/ см. ответы там . Или посмотрите скопированные и вставленные ответы ниже :)   -  person Greg Ennis    schedule 30.12.2014
comment
спасибо за ссылку..   -  person Michael Shrestha    schedule 30.12.2014


Ответы (3)


Я думаю, это из-за ограничения размера текста. Максимально допустимое количество символов — 159. Поэтому вам нужно разделить сообщение на части и отправить его.

person Seshu Vinay    schedule 30.12.2014

// попробуйте использовать этот сервис для отправки коротких или длинных сообщений

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

     }
person MSR    schedule 30.12.2014

Чувак, ты получаешь исключение NullPointerException. Похоже, вы не инициализировали объект sms. Пожалуйста, проверьте..

person Dayanand Fagare    schedule 30.12.2014