时间:2011-09-29 08:58:50

标签: android service alarmmanager android-alarms

我使用闹钟服务来启动通知。我想根据pendingIntent调用它的通知中有不同的图像。我尝试通过意图传递此ID,但应用程序崩溃

        Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
        //pass something to intent
        myIntent.putExtra("param", 999);

        pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);

在服务类中,我通过

捕获它
int test =  intent.getExtras().getInt("param", -1);
Toast.makeText(this, test, Toast.LENGTH_SHORT).show();//the app crashes here!

然而,应用程序在标记的行中崩溃。

我应该如何将图片ID传递给服务,以便我可以使用此ID在规范中设置图片?

修改

这是发生错误时的logcat

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

1 个答案:

答案 0 :(得分:0)

Toast.makeText(this, test, Toast.LENGTH_SHORT).show();//the app crashes here!

表示您正在发送一个int作为消息。这反过来意味着您要告诉Android查找资源ID为999的String。 将其更改为:

Toast.makeText(this, String.valueOf(test), Toast.LENGTH_SHORT).show();//the app won't crash here