Android 拔下辅助线时,AlertDialog 不会关闭

Android AlertDialog won't dismiss when aux cord is unplugged

我希望在用户插入辅助线时显示 AlertDialog 消息。当用户拔下辅助线时,我希望警告消息消失。我不希望用户在 he/she 拔下辅助线之前能够摆脱 AlertDialog 消息。到目前为止,我的 BroadcastReceiverToast 一起工作。但它不适用于 AlertDialog.

import android.app.AlertDialog;
import android.content.BroadcastReceiver; 
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.widget.Toast;


 public class AuxChangeReceiver extends BroadcastReceiver {

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

     AlertDialog.Builder removeAux = new AlertDialog.Builder(context);
        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
            final int state = intent.getIntExtra("state", -1);
            switch (state) {
            case 0:
                removeAux.setOnDismissListener( new OnDismissListener() {
                    public void onDismiss(DialogInterface dialog) {
                        if (state==0) {
                            dialog.dismiss();
                        }
                    }
                });

                break;
            case 1:
                removeAux.setMessage("Unplug Aux!").create().show();
                break;
            default:
                Toast toast3 = Toast.makeText(context, "No clue", Toast.LENGTH_SHORT);
                toast3.show();
            }
        }
    }
}

这是我使用 Toast 的代码,但我不想要 Toast:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;


 public class AuxChangeReceiver extends BroadcastReceiver {

 @Override 
 public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
            final int state = intent.getIntExtra("state", -1);
            switch (state) {
            case 0:
                Toast toast1 = Toast.makeText(context, "There is no aux cord detected", Toast.LENGTH_SHORT);
                toast1.show();
                break;
            case 1:
                Toast toast2 = Toast.makeText(context, "Aux Cord Detected, Please unplug it!", Toast.LENGTH_SHORT);
                toast2.show();
                break;
            default:
                Toast toast3 = Toast.makeText(context, "No clue", Toast.LENGTH_SHORT);
                toast3.show();
            }
        }
    }
}

你必须像这样为警告对话框创建一个方法

 public AlertDialog CreateAlert(String title, String message) {
        AlertDialog alert = new AlertDialog.Builder(this).create();
        alert.setTitle(title);
        alert.setMessage(message);
        alert.show();
        return alert;

    }

通过调用此方法在您想要的地方触发警报

CreateAlert("YOUR TITTLE", "YOUR MESSAGE");

解决方案是在 onCreate() 中创建您的 AlertDialog 而不是在接收器中,这样您就可以在 activity 中的任何地方显示和关闭它 - 类似于下列的。首先像这样声明您的 AlertDialog

public class MainActivity extends Activity  {
    private AlertDialog alertDialog;

然后在 onCreate():

alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Warning");
alertDialog.setMessage("Aux cord plugged in, please unplug...");

然后当插入辅助线时 (case 1:):

alertDialog.show();

当电源线被拔掉时 (case 0:):

alertDialog.dismiss();