How to pass image id to alarm service?
I use alarm service to start notifications. I want to have different image in notification based on which pendingIntent has called it. I tried passing this id via intent, but the app crashes
Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
//pass something to intent
myIntent.putExtra("param", 999);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);
And in service class, I catch it via
int test = intent.getExtras().getInt("param", -1);
Toast.makeText(this, test, Toast.LENGTH_SHORT).show();//the app crashes here!
However, the app crashes in the marked line.
How should I pass image id to the service so that I can use this ID to set image in the norifications?
EDIT
Here is the logcat when error happens
09-29 12:04:45.087: ERROR/AndroidRuntime(12973): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start service com.exercise.AndroidAlarmService.MyAlarmService@2f9768a0 with Intent { flg=0x4 cmp=com.exercise.AndroidAlarmService/.MyAlarmService (has extras) }: android.content.res.Resources$NotFoundException: String resource ID #0x3e7
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063)
at android.app.ActivityThread.access$3600(ActivityThread开发者_如何学运维.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x3e7
at android.content.res.Resources.getText(Resources.java:208)
at android.widget.Toast.makeText(Toast.java:258)
at com.exercise.AndroidAlarmService.MyAlarmService.onStart(MyAlarmService.java:53)
at android.app.Service.onStartCommand(Service.java:420)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
... 10 more
Toast.makeText(this, test, Toast.LENGTH_SHORT).show();//the app crashes here!
Means you are sending an int as the message. This in turn means that you're telling Android to look for a String with resource id 999. Change it to:
Toast.makeText(this, String.valueOf(test), Toast.LENGTH_SHORT).show();//the app won't crash here
精彩评论