Untitled

 avatar
unknown
plain_text
a year ago
2.6 kB
2
Indexable
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MyActivity extends AppCompatActivity {
    private AlertDialog alertDialog;
    private BroadcastReceiver broadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Register broadcast receiver to receive intents
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // Handle the received intent
                handleIncomingIntent(intent);
            }
        };
        registerReceiver(broadcastReceiver, new IntentFilter("your_action_filter"));
    }

    // Method to handle incoming intents
    private void handleIncomingIntent(Intent intent) {
        // Dismiss current dialog if showing
        dismissDialog();

        // Process the new intent and display new dialog(s)
        // For example:
        showDialog();
    }

    // Method to show your dialog
    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("My Dialog")
               .setMessage("This is a dialog message.")
               .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       // Handle positive button click
                   }
               })
               .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       // Handle negative button click
                   }
               });
        alertDialog = builder.create();
        alertDialog.show();
    }

    // Method to dismiss the dialog if it's showing
    private void dismissDialog() {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Unregister broadcast receiver
        if (broadcastReceiver != null) {
            unregisterReceiver(broadcastReceiver);
        }
        // Dismiss dialog if it's showing
        dismissDialog();
    }
}
Editor is loading...
Leave a Comment