Intent String extra 是 SpannbleString,但实际上不是

Intent String extra is SpannbleString, but isn't really

这是我正在使用的广播接收器class:

public class AlarmReciever extends BroadcastReceiver {

private Context context;
private String name;
private String description;

@Override
public void onReceive(Context c, Intent intent) {
    name = intent.getStringExtra("name");
    description = intent.getStringExtra("description");
    context = c;
    Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(1000);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
        .setContentText(description)
        .setSmallIcon(R.drawable.notificationicon)
        .setLights(Color.rgb(78, 155, 222), 1000, 1000)
        .setAutoCancel(true)
        .setContentTitle(name);
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(1, builder.build());
}

}

我的问题是,当我尝试将 Intent 中的 String extras 分配给字符串类型的变量时,出现了以下两个错误:

12-30 20:34:45.224 14614-14614/eli.me.jitteralarm W/Bundle: Key name expected String but value was a android.text.SpannableString. The default value was returned. 12-30 20:34:45.224 14614-14614/eli.me.jitteralarm W/Bundle: Attempt to cast generated internal exception: java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String at android.os.BaseBundle.getString(BaseBundle.java:923) at android.content.Intent.getStringExtra(Intent.java:5349) at eli.me.jitteralarm.AlarmReciever.onReceive(AlarmReciever.java:22) at android.app.ActivityThread.handleReceiver(ActivityThread.java:2725) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

12-30 20:34:45.224 14614-14614/eli.me.jitteralarm W/Bundle: Key description expected String but value was a android.text.SpannableString. The default value was returned. 12-30 20:34:45.224 14614-14614/eli.me.jitteralarm W/Bundle: Attempt to cast generated internal exception: java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String at android.os.BaseBundle.getString(BaseBundle.java:923) at android.content.Intent.getStringExtra(Intent.java:5349) at eli.me.jitteralarm.AlarmReciever.onReceive(AlarmReciever.java:23) at android.app.ActivityThread.handleReceiver(ActivityThread.java:2725) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我知道 getStringExtra 的结果是一个 SpannableString,但是该方法的 return 值是一个字符串(在 intellij 的自动完成和文档中)。

我在 Internet 上浏览了一下,发现您可以尝试将其转换为字符串:

name = "" + spannableString;

但是当我尝试这个时,仍然会抛出错误。

我尝试过的另一个解决方案是在 "spannable string." 上使用 .toString() 当我这样做时,应用程序立即终止并且抛出的错误表明该方法不能用于字符串object。很奇怪。

注意:在调试我的应用程序时,发送的通知不会像上面发布的代码那样具有标题或描述。当我尝试转换方法时,标题和描述都是"null"。这是什么原因?这两个错误都表示它 return 将 null 设为变量而不是所需的标题和描述。

此外,这是我尝试使用广播接收器的代码。我不认为这是问题所在,但以防万一。

public void startAlarm(View view) {
    EditText nameInput = (EditText) this.findViewById(R.id.nameInput);
    EditText descriptionInput = (EditText) this.findViewById(R.id.descriptionInput);
    Intent intent = new Intent(this, AlarmReciever.class);
    intent.putExtra("name", nameInput.getText());
    intent.putExtra("description", descriptionInput.getText());
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            this.getApplicationContext(), 234324243, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent);
}

您需要通过 Intent 输入 String 值,因为 nameInput.getText() 将 return Editable

public void startAlarm(View view) {
   ...
   intent.putExtra("name", nameInput.getText().toString());
   intent.putExtra("description", descriptionInput.getText().toString());
   ...
}

这是对我有用的解决方案:

Bundle extras = getIntent().getExtras();
String contains = new StringBuilder(extras.getCharSequence("contains")).toString();