2012-09-24 29 views
0

在我的任務中,我執行了提醒檢查過程。如果提醒時間與當前時間相等,則會彈出一個彈出框。在此任務中,彈出框正確顯示。需要在android的前屏幕上彈出警報

但是,如果我將這個任務合併到一個大的進程,這意味着提醒任務將是主程序的子程序。彈出窗口不會進入其他屏幕。如果時間是否滿足了當前時間,警報必須顯示給用戶,而用戶使用任何在這個節目的屏幕的..

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); 
           } 

在此警告消息,在需要的時候帶來正面屏幕提醒喚醒。

請幫我找到解決辦法..

在此先感謝。

回答

2

您可以使用下面的代碼掛起的意圖..

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); 

與「機器人。 intent.action.DA「表示DisplayNotification.java類的活動

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(); 
} 
} 

與「android.intent.action.AD」表示AlarmDetails.java類

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")); 
} 

} 

我希望這會幫助你!..

-1

您可以改用狀態欄通知。

+0

它需要在當前運行或前端屏幕。 – gowri

2

如果您試圖詢問當您的活動不是用戶手機上的焦點活動時如何顯示對話框,請嘗試使用通知。通過不同的應用程序彈出對話框會在用戶做其他事情時中斷用戶。從Android UI準則:

Use the notification system — don't use dialog boxes in place of notifications 

If your background service needs to notify a user, use the standard notification system — 
don't use a dialog or toast to notify them. A dialog or toast would immediately 
take focus and interrupt the user, taking focus away from what they were doing: 
the user could be in the middle of typing text the moment the dialog appears 
and could accidentally act on the dialog. 
Users are used to dealing with notifications and 
can pull down the notification shade at their convenience to respond to your message. 

的指南創建的通知是在這裏:http://developer.android.com/guide/topics/ui/notifiers/notifications.html