我做了BroadcastReceiver
。调用onReceive
方法并在控制台上显示Log.i
,但Toast无论如何都不可见。
package com.example.broadcaster;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("STH happened", "STH HAPPENNED");
Toast.makeText(context, "Intent caught\n" + intent.getExtras().getString("MESSAGE"), Toast.LENGTH_LONG);
}
}
我在Log.i
之后得到的日志是:
04-17 21:21:16.742: W/Bundle(1685): Key MESSAGE expected String but value was a android.text.SpannableString. The default value <null> was returned.
04-17 21:21:16.742: W/Bundle(1685): Attempt to cast generated internal exception:
04-17 21:21:16.742: W/Bundle(1685): java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String
04-17 21:21:16.742: W/Bundle(1685): at android.os.Bundle.getString(Bundle.java:1121)
04-17 21:21:16.742: W/Bundle(1685): at com.example.broadcaster.MyReceiver.onReceive(MyReceiver.java:15)
04-17 21:21:16.742: W/Bundle(1685): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2419)
04-17 21:21:16.742: W/Bundle(1685): at android.app.ActivityThread.access$1700(ActivityThread.java:135)
04-17 21:21:16.742: W/Bundle(1685): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
04-17 21:21:16.742: W/Bundle(1685): at android.os.Handler.dispatchMessage(Handler.java:102)
04-17 21:21:16.742: W/Bundle(1685): at android.os.Looper.loop(Looper.java:136)
04-17 21:21:16.742: W/Bundle(1685): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-17 21:21:16.742: W/Bundle(1685): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 21:21:16.742: W/Bundle(1685): at java.lang.reflect.Method.invoke(Method.java:515)
04-17 21:21:16.742: W/Bundle(1685): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-17 21:21:16.742: W/Bundle(1685): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-17 21:21:16.742: W/Bundle(1685): at dalvik.system.NativeStart.main(Native Method)
04-17 21:21:16.752: E/SoundPool(383): error loading /system/media/audio/ui/KeypressReturn.ogg
04-17 21:21:16.762: W/AudioService(383): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
04-17 21:21:16.762: E/SoundPool(383): error loading /system/media/audio/ui/KeypressInvalid.ogg
04-17 21:21:16.762: W/AudioService(383): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
04-17 21:21:16.762: W/AudioService(383): onLoadSoundEffects(), Error -1 while loading samples
编辑:
Brodcasting:
sendBroadcast(new Intent("com.example.MESSAGE_INTENT").putExtra("MESSAGE", ((EditText) findViewById(R.id.textField)).getText()));
最后消息中的为空。
答案 0 :(得分:2)
确保在logcat中获取classCastException,显示代码。
您需要致电show()
以显示祝酒词
Toast.makeText(context, "Intent caught\n" + intent.getExtras().getString("MESSAGE"), Toast.LENGTH_LONG).show();
答案 1 :(得分:1)
你需要在Toast上调用show()。
答案 2 :(得分:0)
EditText上的getText()
返回CharSequence,而不是String。您可以在接收方中拨打getText().toString()
或使用getExtras().getCharSequence()
,具体取决于您是否要保留跨度信息。