0

my app needs to set multiple alarms i would like show custom dialog when the alarm fires.

my receiver class is

    package com.example.memopad;

import java.util.zip.Inflater;








import android.app.Dialog;
import android.app.FragmentManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;



public class AlarmReceiver extends BroadcastReceiver{


     String dato;





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

             dato=intent.getStringExtra("nome");

            lanciaDialog();

            //Toast toast =Toast.makeText(context, "Oggi e' il compleanno di  " + dato , Toast.LENGTH_SHORT);
            // toast.show();





}
    public void lanciaDialog(){

        DialogFragment dialog = new CustomDialogCumple();
        dialog.show(getSupportFragmentManager(), "missiles");

    }



    }

my CustomDialogCumple class is

package com.example.memopad;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;

public class CustomDialogCumple extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.customdialogcumple, null));
        builder.setMessage(R.string.cumple)
               /*.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {

                   }
               })*/
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

i have a problem in dialog.show(getSupportFragmentManager(), "missiles");

i used a Dialogfragment in according with the google guide have you any hint for me ? thanks in advance

1 Answers1

1

What you are trying to do is to display a dialog from a BroadcastReceiver - this is not allowed in Android. You can only show a dialog from an Activity. What you can do is to make the BroadcastReceiver start an Activity and then show the dialog. More information can be read at this blog.

As you can see, he either delivers a notification if the Activity is currently not visible or does something pertinent if the Activity is (which in your case would be to show dialog).

ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • 1
    I do something similar but always need a dialog to appear, so I open a transparent, fullscreen activity with no content. This blank activity then shows the dialog. – cYrixmorten Apr 12 '15 at 10:55
  • You have to have some kind of an activity to show the dialog. It's not possible otherwise. This design pattern leads to the Activity or notification scenario where if you do not have the Activity already running, then you create a notification which can be done from a broadcast receiver. – ucsunil Apr 12 '15 at 11:01