1

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

Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
Mr. N.V.Rao
  • 1,082
  • 13
  • 27

2 Answers2

0

You need an Activity context to show an AlertDialog, so I don't think it is possible to show it in a BroadcastReceiver.

Rachit Mishra
  • 6,101
  • 4
  • 30
  • 51
0

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

ElGavilan
  • 6,610
  • 16
  • 27
  • 36
Venkat
  • 3,447
  • 6
  • 42
  • 61