0

我使用警报服务来启动通知。我想根据哪个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
4

1 回答 1

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

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

Toast.makeText(this, String.valueOf(test), Toast.LENGTH_SHORT).show();//the app won't crash here
于 2011-09-29T10:12:49.877 回答