【问题标题】:How to Display Custom AlertDailog Box inside Broadcast Receiver?如何在广播接收器中显示自定义警报对话框?
【发布时间】:2019-08-12 05:25:59
【问题描述】:

我创建了我想在广播接收器中使用的自定义布局,但getLayoutInflater() 在接收器中不起作用

在 onreceiver 方法中我调用 startAlaram(context,intent)

private void startAlaram(final Context context, Intent intent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        final View view = inflater.inflate(R.layout.custom_layout_notification, null);
        AlertDialog.Builder builder=new AlertDialog.Builder(context,);
        builder.setTitle("Get Reminder");
        builder.setMessage("Alert ALert");
        builder.setIcon(R.mipmap.ic_launcher_round);
        builder.setView(view);
        final AlertDialog  dialog=builder.create();
        TextView not_txt_title,not_txt_desc,not_txt_callfor,not_txt_name;
        ImageView img1;
        not_txt_title = view.findViewById( R.id.not_txt_title );
        not_txt_desc = view.findViewById( R.id.not_txt_desc );
        not_txt_callfor = view.findViewById( R.id.not_txt_callfor );
        not_txt_name = view.findViewById( R.id.not_txt_name );
        img1 = view.findViewById( R.id.img1);

        not_txt_title.setText(rTitle);
        not_txt_desc.setText(rDesc);
        not_txt_callfor.setText(rCallFor);
        not_txt_name.setText(rName);
        dialog.show();
    }

【问题讨论】:

    标签: android-layout android-intent broadcastreceiver android-service receiver


    【解决方案1】:

    您可以使用 AlertDialog 实现透明活动。

    【讨论】:

      【解决方案2】:

      为您的AlertDialog 创建一个void

      private void alert() {
              AlertDialog.Builder builder = new AlertDialog.Builder(this);
              //... your alert stuff
      
          }
      

      在您的Receiver 中显示您的AlertDialog

      @Override
          public void onReceive(Context context, Intent intent) {
      
              alert();
      
          }
      

      【讨论】:

      • 您需要在此活动中使用 Theme.AppCompat 主题(或后代)。这个错误是因为我使用内部接收器造成的。
      • @CodeInsideCofee 您可以在显示 AlertDialog 的位置创建一个空白,而不是使用 this 的上下文。然后你调用 Receiver 中的 void。
      • @CodeInsideCofee 这是什么意思?有什么错误吗?
      • Caused by: java.lang.IllegalStateException: 您需要在此活动中使用 Theme.AppCompat 主题(或后代)。
      • @Jakob,创建这样的对话框AlertDialog.Builder dialog = new AlertDialog.Builder(context, R.style.Theme_AppCompat_Light);
      【解决方案3】:

      这很简单。

      private void showDialog(){
          LayoutInflater factory = LayoutInflater.from(this);
          final View deleteDialogView = factory.inflate(R.layout.mylayout, null);
          final AlertDialog deleteDialog = new AlertDialog.Builder(this).create();
          deleteDialog.setView(deleteDialogView);
          deleteDialogView.findViewById(R.id.yes).setOnClickListener(new OnClickListener() {    
              @Override
              public void onClick(View v) {
                  //your business logic 
                  deleteDialog.dismiss();
              }
          });
          deleteDialogView.findViewById(R.id.no).setOnClickListener(new OnClickListener() {    
              @Override
              public void onClick(View v) {
                  deleteDialog.dismiss();    
              }
          });
      
          deleteDialog.show();
      }
      

      *您必须注册/取消注册您的广播。

      registerReceiver(mReceiver, new IntentFilter("com.mybroadcast"));
      unregisterReceiver(mReceiver);
      

      你的广播代码是这样的。

      Broadcast mReceiver = new Broadcast(){
      
      @Override
          public void onReceive(Context context, Intent intent) {
               showDialog()
          }
      
      }
      

      【讨论】:

      • 亲爱的朋友,您的简单解决方案在接收器内部不起作用。
      • 1. /*删除这个*/ builder.setView(view); . 2.最终AlertDialog对话框=builder.create(); 3.添加这个dialog.setView(view);
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      相关资源
      最近更新 更多