简体   繁体   中英

How to show alert dialog in home screen?

I am trying to run alarm which make alert dialog run in home screen showing . ie when the application is closed, and when the event time is reached, the alert dialog shows the title of the event and rings alarm infinitely. If the button in alert dialog is clicked, then the alarm goes off, My code is as follows:

void doReminderWork(Intent intent) {
    ToDoApplicationActivity ap = new ToDoApplicationActivity();
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent notificationIntent = new Intent(this, ToDoApplicationActivity.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);

    Notification note = new Notification(R.drawable.alarm, "...Calendar Alarm...", System.currentTimeMillis());
    note.setLatestEventInfo(this,"Event : ", ap.title1, pi);

    note.defaults |= Notification.DEFAULT_SOUND;
    note.flags |= Notification.FLAG_AUTO_CANCEL;

    int id = 123456789;
    manager.notify(id, note);


   Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
    mMediaPlayer = new MediaPlayer();

    try {
        mMediaPlayer.setDataSource(this, alert);
    } catch (IllegalArgumentException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (SecurityException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IllegalStateException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

    if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0)
    {
               mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
               mMediaPlayer.setLooping(false);
               try 
               {
                   mMediaPlayer.prepare();
               }
               catch (IllegalStateException e) 
               {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            mMediaPlayer.start();

     }

After this if i give alert dialog code to stop media player, i know it cant stop playing the alarm. Here, even when the application is closed, i need to show the alert dialog. I m trying this for past 2 days.

I'm Getting Following Exception:

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

Any help is highly appreciated and thanks in advance...

When you get alarm notification then you can show alert dialog in the activity which has no view by starting the activity. This is what you wanted.?

By showing alert in the activity with no view you will see only alert dialog on the home screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM