我发现在搜索短信时,如果它太长,它可能会返回一个空指针异常。我已将其更改为 sendMultipartTextMessage 但我仍然收到空指针异常。此外,问题在于消息没有分成多个部分。
这是我的代码:
private void sendSMS(String phoneNumber, String message) {
System.out.println("SMS: " + message);
System.out.println("to " + phoneNumber);
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();
Log.d("SMSTest", "Sending '" + message + "' in multiple parts.");
ArrayList<String> parts = sms.divideMessage(message);
Log.d("SMSTest", parts.size() + " parts:");
for (String string : parts) {
Log.d("SMSTest", string);
}
ArrayList<PendingIntent> sentList = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveredList = new ArrayList<PendingIntent>();
for (int i = 0; i < parts.size(); i++) {
sentList.add(sentPI);
deliveredList.add(deliveredPI);
}
sms.sendMultipartTextMessage(phoneNumber, null, parts, sentList,
deliveredList);
}
这是我的日志:
12-21 17:53:08.291: D/SMSTest(12578): Sending ' .. content of the message...'
12-21 17:53:08.311: D/SMSTest(12578): 1 parts:
12-21 17:53:08.321: E/AndroidRuntime(12578): FATAL EXCEPTION: main
12-21 17:53:08.321: E/AndroidRuntime(12578): java.lang.NullPointerException
12-21 17:53:08.321: E/AndroidRuntime(12578): at android.os.Parcel.readException(Parcel.java:1328)
12-21 17:53:08.321: E/AndroidRuntime(12578): at android.os.Parcel.readException(Parcel.java:1276)
12-21 17:53:08.321: E/AndroidRuntime(12578): at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:613)
12-21 17:53:08.321: E/AndroidRuntime(12578): at android.telephony.SmsManager.sendTextMessage(SmsManager.java:109)
12-21 17:53:08.321: E/AndroidRuntime(12578): at android.telephony.SmsManager.sendMultipartTextMessage(SmsManager.java:269)
12-21 17:53:08.321: E/AndroidRuntime(12578): at android.telephony.gsm.SmsManager.sendMultipartTextMessage(SmsManager.java:131)
12-21 17:53:08.321: E/AndroidRuntime(12578): at com.example.senddemo.MainActivity.sendSMS(MainActivity.java:358)
12-21 17:53:08.321: E/AndroidRuntime(12578): at com.example.senddemo.MainActivity.access$0(MainActivity.java:288)
12-21 17:53:08.321: E/AndroidRuntime(12578): at com.example.senddemo.MainActivity$2.onClick(MainActivity.java:119)
12-21 17:53:08.321: E/AndroidRuntime(12578): at android.view.View.performClick(View.java:2538)
12-21 17:53:08.321: E/AndroidRuntime(12578): at android.view.View$PerformClick.run(View.java:9152)
12-21 17:53:08.321: E/AndroidRuntime(12578): at android.os.Handler.handleCallback(Handler.java:587)
12-21 17:53:08.321: E/AndroidRuntime(12578): at android.os.Handler.dispatchMessage(Handler.java:92)
12-21 17:53:08.321: E/AndroidRuntime(12578): at android.os.Looper.loop(Looper.java:130)
12-21 17:53:08.321: E/AndroidRuntime(12578): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-21 17:53:08.321: E/AndroidRuntime(12578): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 17:53:08.321: E/AndroidRuntime(12578): at java.lang.reflect.Method.invoke(Method.java:507)
12-21 17:53:08.321: E/AndroidRuntime(12578): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-21 17:53:08.321: E/AndroidRuntime(12578): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-21 17:53:08.321: E/AndroidRuntime(12578): at dalvik.system.NativeStart.main(Native Method)