I'm attempting to create a service which sends a text message containing current wifi usage however I'm continually getting an error stating "Illegal Argument Exception: Invalid message body".
Any suggestions?
SOURCE:
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
EDIT AFTER FIRST RESPONSE:
@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);
}
}
UPDATED LOGCAT (AFTER FIRST RESPONSE)
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
You got that error due to an empty message.
Here it is:
String info = "";
String sms = info;
After the below code, the info
and sms
are the same. But after you perform this operation:
info += "Monthly Wifi Data Usage Amount:\t";
a new String is created and assigned to info
, so info
and sms
are not the same anymore. The sms
is still empty, and thats why you got IllegalArgumentException.
Just remove info
in your code and only use the sms
variable. You can also use
StringBuilder