我有一个 smsReceiver,每当我收到一条短信时,我都想触发一个弹出窗口。每当有短信来临时,我的 smsreceiver 方法就会被触发。然后我尝试调用一个对话框
@overide //of smsreciver class
public void onReceive(Context context, Intent intent){
showNotificattion(context)
}
private void showNotificattion(Context context){
Log.d("sms","showing notification now 2");
// Create out AlterDialog
Builder builder = new AlertDialog.Builder(context);
//even tried context.getApplicationcontext()
builder.setMessage("This will end the activity");
builder.setCancelable(true);
builder.setPositiveButton("I agree", new OkOnClickListener());
builder.setNegativeButton("No, no", new CancelOnClickListener());
AlertDialog dialog = builder.create();
dialog.show();
}
但是每当收到短信时,我都会关注
07-25 14:02:04.235: E/AndroidRuntime(17935): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
07-25 14:02:04.235: E/AndroidRuntime(17935): at android.view.ViewRoot.setView(ViewRoot.java:566)
07-25 14:02:04.235: E/AndroidRuntime(17935): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:179)
07-25 14:02:04.235: E/AndroidRuntime(17935): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-25 14:02:04.235: E/AndroidRuntime(17935): at android.app.Dialog.show(Dialog.java:265)
07-25 14:02:04.235: E/AndroidRuntime(17935): at com.example.xsms.SmsReceiver.showNotificattion(SmsReceiver.java:92)
07-25 14:02:04.235: E/AndroidRuntime(17935): at com.example.xsms.SmsReceiver.doTask(SmsReceiver.java:73)
07-25 14:02:04.235: E/AndroidRuntime(17935): at com.example.xsms.SmsReceiver.onReceive(SmsReceiver.java:44)
07-25 14:02:04.235: E/AndroidRuntime(17935): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2103)
07-25 14:02:04.235: E/AndroidRuntime(17935): ... 10 more
如何解决这个问题?我想将我的应用程序作为后台服务运行,并且该对话框应该可见