Search code examples
androidbroadcastreceiverandroid-broadcast

Alert Dialog is not working in Broad Cast Receiver


I am using Broad Cast Receiver For Alarm Manager to Send the Message to Device. For this I have write the Broad Cast Receiver Class. And also i want to display the Alert Dialog in the Receive method. For this the Broad cast Receiver as follows. And I am including the Alert Dialog in the Broad Cast Receiver.

public class AlarmReciever extends BroadcastReceiver
{
         @Override
            public void onReceive(Context context, Intent intent)
            {


                    String phoneNumberReciver="5556";
                    String message="Happy Birthday My dear Friend";
                    SmsManager sms = SmsManager.getDefault(); 
                    sms.sendTextMessage(phoneNumberReciver, null, message, null, null);
                    Toast.makeText(context, "Alarm fired and SMS Sent", Toast.LENGTH_LONG).show();


                    AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
                    builder1.setMessage("message delivered");
                    builder1.setCancelable(true);
                    builder1.setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
                    builder1.setNegativeButton("No",
                            new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

                    AlertDialog alert11 = builder1.create();
                    alert11.show();


             }

}

And I am calling the broadcast Receiver from Activity

Long time = new GregorianCalendar().getTimeInMillis()+00*00*03*1000;
Intent intentAlarm = new Intent(this, AlarmReciever.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1,  intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));

And I am getting the error in the Broad cast receiver class in the alert1.show() line


Solution

  • enter image description here

    Create an Activity like this way:

    public class MainActivity extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Message")
               .setTitle("Title")
               .setCancelable(false)
               .setPositiveButton("OK", new DialogInterface.OnClickListener()
               {
                   public void onClick(DialogInterface dialog, int id) 
                   {
                       MainActivity.this.finish();
                   }
               });
    
    
        AlertDialog alert = builder.create();
        alert.show();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }}
    

    Create theme:

    <resources>
            <style name="Theme.Transparent" parent="android:Theme">
                <item name="android:windowIsTranslucent">true</item>
                <item name="android:windowBackground">@android:color/transparent</item>
                <item name="android:windowContentOverlay">@null</item>
                <item name="android:windowNoTitle">true</item>
                <item name="android:windowIsFloating">true</item>
                <item name="android:backgroundDimEnabled">false</item>
              </style> 
    </resources>
    

    Set this theme in your manifest file:

    <activity
            android:name=".MainActivity"
            android:theme="@style/Theme.Transparent"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    

    Call this MainActivity class from your BroadCastReceiver Class