我正在提取推送通知的文本并在警报中显示 - 但是,我在激活接收器时遇到致命异常(请参阅下面的logcat)
11-26 08:35:51.919 4607-4607/au.gov.nsw.shellharbour.saferroadsshellharbour E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start receiver au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2291)
at android.app.ActivityThread.access$1600(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4963)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:716)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:356)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:234)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:153)
at android.app.Dialog.show(Dialog.java:277)
at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
at au.gov.nsw.shellharbour.saferroadsshellharbour.HomeScreen$Receiver.onPushOpen(HomeScreen.java:198)
at com.parse.ParsePushBroadcastReceiver.onReceive(ParsePushBroadcastReceiver.java:108)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2284)
at android.app.ActivityThread.access$1600(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4963)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
11-26 08:36:06.779 4607-4607/au.gov.nsw.shellharbour.saferroadsshellharbour I/Process﹕ Sending signal. PID: 4607 SIG: 9
以下是我的Receiver课程:
private void displayAlert(){
AlertDialog Alert= new AlertDialog.Builder(this)
.setTitle("News")
.setMessage(notificationText)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
}
})
.show();
}
public static class Receiver extends ParsePushBroadcastReceiver {
private String notificationText;
public Receiver(){
}
private static final String TAG = "MyNotificationsReceiver";
@Override
public void onPushOpen(Context context, Intent intent) {
Log.e("Push", "Clicked");
Intent i = new Intent(context, HomeScreen.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
try {
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
notificationText = json.getString("alert");
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
}
}
}
更新:现在我可以打开活动没问题,但无法提取推送通知消息,将其置于提醒对话框中,并显示提醒对话框。
答案 0 :(得分:1)
给予BroadcastReceiver的Context无法显示Dialog。
听起来你应该遵循使用NotificationManager显示通知的标准模式。这是更标准的体验以及您的用户期望的内容。
以下链接将显示通知示例的文档,并且需要阅读整页以了解可用的不同选项。
http://developer.android.com/guide/topics/ui/notifiers/notifications.html#SimpleNotification
答案 1 :(得分:1)
public class scb998 extends Application { // this could be any singleton class not just application
static boolean scb988b; // its false by default, but feel free to instatiate it
static String msg = ""; // this came after the edit
}
然后在你的接收器类之后context.startActivity(i);
触发你的布尔值为true ..然后按照我的评论...实际上就是它..你也可以通过意图函数putString将字符串传递给活动一个静态单例字符串变量无论如何Sir ..
实际上这只是代表...大声笑..
在您的最后评论后编辑
整个故事是你正在尝试显示一个对话框,当收到一个消息时 - 但是你不能在你的接收器中这样做,所以你所做的就是与你的活动沟通时,让它知道一个收到了新的msg,有很多方法可以做到,从otto开始到local broadcast manager,您可以自己学习这些,或者使用我的这种简单方法,扩展应用程序类,并且像我一样放置一个静态布尔..不要把它放在Receiver
你也可以为你的noty消息Scb998.msg = notificationText = json.getString("alert");
创建一个静态字符串,然后在context.startActivity(i);
之后设置你的布尔值Scb998 .scb988b = true ;,之后删除并将其放入您的活动
void calalert(){
AlertDialog Alert= new AlertDialog.Builder(this)
.setTitle("News")
.setMessage(Scb998.msg)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
}
})
.show();
Scb998.scb988b = false;
}
现在在你的Home类中调用它onresume并检查boolean
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if(Scb998.scb988b)
calalert();
}
答案 2 :(得分:0)
只需使用YOUR-ACTIVITY-NAME.this
作为警报构建器上下文。
更正后的表格,例如:
AlertDialog Alert= new AlertDialog.Builder(YOUR-ACTIVITY-NAME.this)