How to display custom dialog on Android home screen

To show custom dialog on Android home screen we need to have 3 different components in the application.

1) Alarm manager to trigger alarm.
2) Receiver for receiving the alarm.
3) Custom Activity Dialog.

In our example on click of button you need to wait for 1 minutes. By that time quit the app and go to home screen. Alarm manager triggers & displays the custom dialog. In Sample code it triggers receiver after specific duration i.e 1 minutes (60000 milliseconds). logic is  present in mAlertButton.setOnClickListener. 

MainActivity.java

 public class MainActivity extends ActionBarActivity {  
      Button mAlertButton;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     mAlertButton = (Button)findViewById(R.id.ID_Alert_dialog);  
     mAlertButton.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View arg0) {  
                     
                     //Alarm manager                      
                     
                        long timestamp = 60000;	
		        Intent intent = new Intent(getApplicationContext(), AlertDialogReceiver.class);
		        PendingIntent mAlarmSender = PendingIntent.getBroadcast(getApplicationContext(),
		        		167, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		        
		        AlarmManager amgr = (AlarmManager) getApplicationContext()
		                .getSystemService(getApplicationContext().ALARM_SERVICE);
		        
		        amgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + timestamp,
		        		mAlarmSender); 
                }  
           });  
   }  
 }  

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   tools:context="com.example.alertdialogexample.MainActivity" >  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Alert dialog comes after 1 mins"  
     android:id="@+id/ID_Dialog_message" />  
   <Button   
     android:layout_marginTop="50dp"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:id="@+id/ID_Alert_dialog"  
     android:text ="Enable alert dialog"  
     />  
 </RelativeLayout>  

As soon as alarm triggers, AlertDialogReceiver creates a new intent of Activity dialog.

AlertDialogReceiver.java

 public class AlertDialogReceiver extends BroadcastReceiver{  
      @Override  
      public void onReceive(Context ct, Intent arg1) {  
           //Launch the alertDialog.  

            Intent alarmIntent = new Intent("android.intent.action.MAIN");  
            alarmIntent.setClass(ct, AlertDialogClass.class);  
            alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  

       // Start the popup activity  

      ct.startActivity(alarmIntent);  
           }  
      }  

Showing popup on screen is very simple, create a Activity but don’t set the view, instead inflate a view & set it for dialog.

dialog_layout.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical" >  
   <TextView   
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="This is your popup dialog preview"  
     android:textSize="20sp"  
     />  
   <ImageView   
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:src="@drawable/ic_launcher"/>  
   <LinearLayout  
      android:layout_width="match_parent"  
      android:layout_height="match_parent"  
      android:orientation="horizontal">  
   <Button   
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Ok"  
     android:layout_weight="0.5"  
     android:id="@+id/ID_Ok"/>  
   <Button   
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Cancel"  
     android:layout_weight="0.5"  
     android:id="@+id/ID_Cancel"/>  
   </LinearLayout>  
 </LinearLayout>  

AlertDialogClass.java

 public class AlertDialogClass extends Activity{  
   AlertDialog.Builder mAlertDlgBuilder;  
   AlertDialog mAlertDialog;  
   View mDialogView = null;  
   Button mOKBtn, mCancelBtn;  
      @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  

     //setContentView(R.layout.activity_main);  

     LayoutInflater inflater = getLayoutInflater();
  
     // Build the dialog  
     mAlertDlgBuilder = new AlertDialog.Builder(this);  
        mDialogView = inflater.inflate(R.layout.dialog_layout, null);  
        mOKBtn = (Button)mDialogView.findViewById(R.id.ID_Ok);  
        mCancelBtn = (Button)mDialogView.findViewById(R.id.ID_Cancel);  
        mOKBtn.setOnClickListener(mDialogbuttonClickListener);  
        mCancelBtn.setOnClickListener(mDialogbuttonClickListener);  
        mAlertDlgBuilder.setCancelable(false);  
        mAlertDlgBuilder.setInverseBackgroundForced(true);  
        mAlertDlgBuilder.setView(mDialogView);  
     mAlertDialog = mAlertDlgBuilder.create();  
     mAlertDialog.show(); 
 
   }  
 OnClickListener mDialogbuttonClickListener = new OnClickListener() {  
           @Override  
           public void onClick(View v) {  
                if(v.getId() == R.id.ID_Ok)  
                {  
                     mAlertDialog.dismiss();  
                     finish();  
                }  
                else if(v.getId() == R.id.ID_Cancel)  
                {  
                     mAlertDialog.dismiss();  
                     finish();  
                }  
           }  
  };  
 }  

Add the receiver in Manifest file.

   <receiver android:name="com.example.alertdialogexample.AlertDialogReceiver"/>