Intent String extra是SpannbleString,但不是

时间:2015-12-31 01:43:14

标签: android string android-intent spannablestring

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

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());
}

}

我的问题是,当我尝试将意图中的String附加内容分配给类型字符串的变量时,我得到以下两个错误:

  

12-30 20:34:45.224 14614-14614 / eli.me.jitteralarm W / Bundle:Key name expected String但value是android.text.SpannableString。返回了默认值。   12-30 20:34:45.224 14614-14614 / eli.me.jitteralarm W / Bundle:尝试转换生成的内部异常:                                                               java.lang.ClassCastException:android.text.SpannableString无法强制转换为java.lang.String                                                                   在android.os.BaseBundle.getString(BaseBundle.java:923)                                                                   在android.content.Intent.getStringExtra(Intent.java:5349)                                                                   在eli.me.jitteralarm.AlarmReciever.onReceive(AlarmReciever.java:22)                                                                   在android.app.ActivityThread.handleReceiver(ActivityThread.java:2725)                                                                   在android.app.ActivityThread.-wrap14(ActivityThread.java)                                                                   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1421)                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                   在android.os.Looper.loop(Looper.java:148)                                                                   在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

     

12-30 20:34:45.224 14614-14614 / eli.me.jitteralarm W / Bundle:Key   描述期望字符串但值是a   android.text.SpannableString。返回了默认值。   12-30 20:34:45.224 14614-14614 / eli.me.jitteralarm W / Bundle:尝试   cast生成的内部异常:                                                               java.lang.ClassCastException:android.text.SpannableString不能   强制转换为java.lang.String                                                                   在android.os.BaseBundle.getString(BaseBundle.java:923)                                                                   在android.content.Intent.getStringExtra(Intent.java:5349)                                                                   在eli.me.jitteralarm.AlarmReciever.onReceive(AlarmReciever.java:23)                                                                   在android.app.ActivityThread.handleReceiver(ActivityThread.java:2725)                                                                   在android.app.ActivityThread.-wrap14(ActivityThread.java)                                                                   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1421)                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                   在android.os.Looper.loop(Looper.java:148)                                                                   在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

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

我在互联网上浏览过,发现你可以通过尝试将它转换为字符串:

name = "" + spannableString;

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

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

注意:在调试我的应用程序时,正在发送的通知将没有上面发布代码的方式的标题或说明。当我尝试转换方法时,标题和描述都是" null"。这是什么原因?两个错误都表示它返回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);
}

2 个答案:

答案 0 :(得分:0)

您需要通过String添加Intent值,因为nameInput.getText()将返回Editable

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

答案 1 :(得分:0)

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

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