我在发送短信时大部分时间都在三星设备上遇到这个问题。
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()
方法内。
有人可以帮我吗?
*注意:我的应用程序在后台发送短信,并且它不是默认的短信应用程序。
答案 0 :(得分:1)
我想这是因为文字超出限制。允许的最大字符数为159.因此,您必须将消息分成几部分并发送。
答案 1 :(得分:1)
//尝试使用此服务发送短消息或长消息
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);
}
答案 2 :(得分:0)
老兄你得到的是NullPointerException。好像你没有初始化短信对象。 请检查..