繁体   English   中英

如何在BroadcastReceiver中显示AlertDialog

[英]How to display AlertDialog in BroadcastReceiver

我试图在BroadcastReceiver的子类的一个方法中显示AlertDialog。 我的设置是

            if(telisinthedatabase == true){
                 String checkid = dbHelp.getidbySimcardnumber(tel);
                 LayoutInflater mInflater = LayoutInflater.from(thisclasscontext);
                 View statusupdateView = mInflater.inflate(R.layout.statuslayout, null);
                 TextView statustext = (TextView) statusupdateView.findViewById(R.id.statuseditText);
                 String text[] = msg.split("\n");
                 statustext.setText(text[0]+"\n"+text[1]+"\n"+text[2]+"\n"+text[3]);
                 new AlertDialog.Builder(thisclasscontext).setTitle("Current status of the tracker at: "+checkid).setView(statusupdateView)
                 .setPositiveButton("OK", new DialogInterface.OnClickListener() 
                 {                  
                   @SuppressLint("SimpleDateFormat")
                   public void onClick(DialogInterface dialog, int whichButton) 
                   {                    


                   }
                }).show();
            }

我将实例contextonReceive(Context context, Intent intent)方法thisclasscontextthisclasscontext 。但是程序崩溃了。 我的编码出了什么问题? 我在其他活动中使用相同的编码,很好。 谢谢

您无法直接从BroadCastReceiver显示警报,因为没有活动窗口显示对话框。要显示警报,您必须在接收器中启动活动,并且在该活动中您可以显示警报。

之前提到的一切都是正确的。 但是,如果您的接收器是当前前台活动并且您引用上下文,则可以引发AlertDialog - 即您不需要启动新活动。

您无法从广播接收器显示对话框。 仅限活动,且仅当该活动位于前台或您将获得异常时。 没有看到堆栈跟踪很难说,但这可能是你的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM