非法参数例外:发送SMS时邮件正文无效

时间:2013-06-12 20:00:37

标签: android service sms alarm forceclose

我正在尝试创建一个发送包含当前wifi使用情况的文本消息的服务,但是我不断收到错误,指出“非法的参数异常:无效的消息体”。

有什么建议吗?

消息来源:

public class Alarm extends Service {

    @Override
    public void onCreate() {

        // TODO Auto-generated method stub

    }

    @Override
    public IBinder onBind(Intent intent) {

        // TODO Auto-generated method stub

        return null;

    }

    @Override
    public void onStart(Intent intent, int startId) {

        // TODO Auto-generated method stub

        super.onStart(intent, startId);


        String info = "";
        String sms = info;

        info += "Monthly Wifi Data Usage Amount:\t";
        info += ("\tReceived: " + TrafficStats.getTotalRxBytes() + " bytes / "
                + TrafficStats.getTotalRxPackets() + " packets\n");
        info += ("\tTransmitted: " + TrafficStats.getTotalTxBytes()
                + " bytes / " + TrafficStats.getTotalTxPackets() + " packets\n");

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage("9545555555", null, sms, null, null);    

    }

    @Override
    public boolean onUnbind(Intent intent) {

        // TODO Auto-generated method stub

        return super.onUnbind(intent);

    }

}

logcat的:

06-12 15:53:42.395: E/AndroidRuntime(29664): FATAL EXCEPTION: main
06-12 15:53:42.395: E/AndroidRuntime(29664): java.lang.RuntimeException: Unable to start service com.example.wifimonitor.Alarm@428fbb90 with Intent { flg=0x4 cmp=com.example.wifimonitor/.Alarm (has extras) }: java.lang.IllegalArgumentException: Invalid message body
06-12 15:53:42.395: E/AndroidRuntime(29664):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2549)
06-12 15:53:42.395: E/AndroidRuntime(29664):    at android.app.ActivityThread.access$2000(ActivityThread.java:139)
06-12 15:53:42.395: E/AndroidRuntime(29664):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
06-12 15:53:42.395: E/AndroidRuntime(29664):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 15:53:42.395: E/AndroidRuntime(29664):    at android.os.Looper.loop(Looper.java:137)
06-12 15:53:42.395: E/AndroidRuntime(29664):    at android.app.ActivityThread.main(ActivityThread.java:4918)
06-12 15:53:42.395: E/AndroidRuntime(29664):    at java.lang.reflect.Method.invokeNative(Native Method)
06-12 15:53:42.395: E/AndroidRuntime(29664):    at java.lang.reflect.Method.invoke(Method.java:511)
06-12 15:53:42.395: E/AndroidRuntime(29664):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
06-12 15:53:42.395: E/AndroidRuntime(29664):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
06-12 15:53:42.395: E/AndroidRuntime(29664):    at dalvik.system.NativeStart.main(Native Method)
06-12 15:53:42.395: E/AndroidRuntime(29664): Caused by: java.lang.IllegalArgumentException: Invalid message body
06-12 15:53:42.395: E/AndroidRuntime(29664):    at android.telephony.SmsManager.sendTextMessage(SmsManager.java:119)
06-12 15:53:42.395: E/AndroidRuntime(29664):    at com.example.wifimonitor.Alarm.onStart(Alarm.java:59)
06-12 15:53:42.395: E/AndroidRuntime(29664):    at android.app.Service.onStartCommand(Service.java:450)
06-12 15:53:42.395: E/AndroidRuntime(29664):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2532)
06-12 15:53:42.395: E/AndroidRuntime(29664):    ... 10 more

第一次回复后编辑:

    @Override
    public void onCreate() {

        // TODO Auto-generated method stub

    }

    @Override
    public IBinder onBind(Intent intent) {

        // TODO Auto-generated method stub

        return null;

    }

    @Override
    public void onStart(Intent intent, int startId) {

        // TODO Auto-generated method stub

        super.onStart(intent, startId);

        String phoneNumber = "9545555555";
        String sms = "";
    //  String sms = info;

        sms += "Monthly Wifi Data Usage Amount:\t";
        sms += ("\tReceived: " + TrafficStats.getTotalRxBytes() + " bytes / "
                + TrafficStats.getTotalRxPackets() + " packets\n");
        sms += ("\tTransmitted: " + TrafficStats.getTotalTxBytes()
                + " bytes / " + TrafficStats.getTotalTxPackets() + " packets\n");

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage("9545555555", null, sms, null, null);

    }

    @Override
    public boolean onUnbind(Intent intent) {

        // TODO Auto-generated method stub

        return super.onUnbind(intent);

    }

}

更新的LOGCAT(第一次回应后)

06-12 16:32:49.645: E/AndroidRuntime(31197): FATAL EXCEPTION: main
06-12 16:32:49.645: E/AndroidRuntime(31197): java.lang.RuntimeException: Unable to start service com.example.wifimonitor.Alarm@42cc0d10 with Intent { flg=0x4 cmp=com.example.wifimonitor/.Alarm (has extras) }: java.lang.NullPointerException
06-12 16:32:49.645: E/AndroidRuntime(31197):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2549)
06-12 16:32:49.645: E/AndroidRuntime(31197):    at android.app.ActivityThread.access$2000(ActivityThread.java:139)
06-12 16:32:49.645: E/AndroidRuntime(31197):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
06-12 16:32:49.645: E/AndroidRuntime(31197):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 16:32:49.645: E/AndroidRuntime(31197):    at android.os.Looper.loop(Looper.java:137)
06-12 16:32:49.645: E/AndroidRuntime(31197):    at android.app.ActivityThread.main(ActivityThread.java:4918)
06-12 16:32:49.645: E/AndroidRuntime(31197):    at java.lang.reflect.Method.invokeNative(Native Method)
06-12 16:32:49.645: E/AndroidRuntime(31197):    at java.lang.reflect.Method.invoke(Method.java:511)
06-12 16:32:49.645: E/AndroidRuntime(31197):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
06-12 16:32:49.645: E/AndroidRuntime(31197):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
06-12 16:32:49.645: E/AndroidRuntime(31197):    at dalvik.system.NativeStart.main(Native Method)
06-12 16:32:49.645: E/AndroidRuntime(31197): Caused by: java.lang.NullPointerException
06-12 16:32:49.645: E/AndroidRuntime(31197):    at android.os.Parcel.readException(Parcel.java:1431)
06-12 16:32:49.645: E/AndroidRuntime(31197):    at android.os.Parcel.readException(Parcel.java:1379)
06-12 16:32:49.645: E/AndroidRuntime(31197):    at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:714)
06-12 16:32:49.645: E/AndroidRuntime(31197):    at android.telephony.SmsManager.sendTextMessage(SmsManager.java:203)
06-12 16:32:49.645: E/AndroidRuntime(31197):    at com.example.wifimonitor.Alarm.onStart(Alarm.java:44)
06-12 16:32:49.645: E/AndroidRuntime(31197):    at android.app.Service.onStartCommand(Service.java:450)
06-12 16:32:49.645: E/AndroidRuntime(31197):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2532)
06-12 16:32:49.645: E/AndroidRuntime(31197):    ... 10 more

1 个答案:

答案 0 :(得分:3)

由于空信息,您收到了该错误。

这是:

    String info = "";
    String sms = info;

在以下代码之后,infosms是相同的。但是在执行此操作后:

    info += "Monthly Wifi Data Usage Amount:\t";

创建了一个新的字符串并将其分配给info,因此infosms不再相同。 sms仍然是空的,这就是你得到IllegalArgumentException的原因。

只需删除代码中的info,然后只使用sms变量。你也可以使用 StringBuilder