Search code examples
androidandroid-alertdialogreminders

alert needs to pop-up on front screen in android


In my task, I have perform the Reminder Checking process. If the reminder time comes to equal the Current time it will raise a pop-up box. In this Task the pop-up box Comes correctly.

But if i Merge this task to a Some big process, Which means the Reminder task will be a Sub-program of the main program. The Pop-up not coming in the other Screens. If the Time Met the Current time, the alert must shown to the User, while the user using any of the Screen in this Program..

if (LDbTime <= LSysTime) {
                                    rem_id = c.getString(c.getColumnIndex("reminder_id"));
                                    remName = c.getString(c.getColumnIndex("rname"));
                                    mediaPlayer.start();
                                    handler.post(new Runnable(){
                                    public void run() {
                                        alert.setTitle("Alert :"+remName);
                                        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int whichButton) {
                                                mediaPlayer.pause();
                                            }
                                        });
                                        alert.show();
                                        db1.execSQL("UPDATE RemainAlarmS SET expired ='TRUE' WHERE reminder_id = " + rem_id );
                                            }
                                        });
                                    Thread.sleep(5000);
                                }

In this Alert message needs to Bring for Front Screen at the time of Reminder wake-up.

Please Help me to find the Solution..

Thanks in Advance.


Solution

  • You Can use the Following codes for the Pending Intent..

    Intent i = new Intent("android.intent.action.DA");
    PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
    AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
    GregorianCalendar calendar = new GregorianCalendar(y,m,d, hr, mi);
    long alarm_time = calendar.getTimeInMillis();
    alarmManager.set(AlarmManager.RTC_WAKEUP  , alarm_time , operation);
    

    And the "android.intent.action.DA" denotes the Activity of the DisplayNotification.java class

    public class DisplayNotification extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        int notifID = getIntent().getExtras().getInt("NotifID");
        Intent i = new Intent("android.intent.action.AD");
        i.putExtra("NotifID", notifID);  
        PendingIntent detailsIntent =PendingIntent.getActivity(this, 0, i, 0);
        NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        Notification notif = new Notification(R.drawable.ic_launcher,"Time's up!",System.currentTimeMillis());
        CharSequence from = "AlarmManager - Time's up!";
        CharSequence message = "This is your alert, courtesy of the AlarmManager";        
        notif.setLatestEventInfo(this, from, message, detailsIntent);
        notif.vibrate = new long[] { 100, 250, 100, 500};        
        nm.notify(notifID, notif);
        finish();
    }
    }
    

    And the "android.intent.action.AD" denotes the AlarmDetails.java class

    public class AlarmDetails extends Activity {
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alarmdetails);
    
        NotificationManager nm = (NotificationManager) 
                getSystemService(NOTIFICATION_SERVICE);
            //---cancel the notification---
            nm.cancel(getIntent().getExtras().getInt("NotifID")); 
    }
    
    }
    

    I hope this will help you!..