在android中,需要在前屏幕上弹出警报

在android中,需要在前屏幕上弹出警报,android,android-alertdialog,reminders,Android,Android Alertdialog,Reminders,在我的任务中,我执行了提醒检查过程。如果提醒时间等于当前时间,则会弹出一个弹出框。在此任务中,弹出框正确显示 但是如果我把这个任务合并到一个大进程中,这意味着提醒任务将是主程序的一个子程序。弹出窗口不会出现在其他屏幕中。如果时间与当前时间一致,则当用户使用此程序中的任何屏幕时,必须向用户显示警报 if (LDbTime <= LSysTime) { rem_id = c.getString(c.getColumnInd

在我的任务中,我执行了提醒检查过程。如果提醒时间等于当前时间,则会弹出一个弹出框。在此任务中,弹出框正确显示

但是如果我把这个任务合并到一个大进程中,这意味着提醒任务将是主程序的一个子程序。弹出窗口不会出现在其他屏幕中。如果时间与当前时间一致,则当用户使用此程序中的任何屏幕时,必须向用户显示警报

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

if(LDbTime您可以使用状态栏通知。

您可以使用状态栏通知。

如果您试图询问当您的活动不是用户手机上的重点活动时如何显示对话框,请尝试使用通知。在其他应用程序上弹出对话框会在用户需要时中断该对话框可能正在做其他事情。根据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.

创建通知的指南如下:

如果您试图询问当您的活动不是用户手机上的重点活动时如何显示对话框,请尝试使用通知。在其他应用程序上弹出对话框会在用户可能正在做其他事情时中断用户。根据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.

此处提供了创建通知的指南:

对于挂起的意图,您可以使用以下代码

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);
“android.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();
}
}
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")); 
}

}
“android.intent.action.AD”表示AlarmDetails.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();
}
}
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")); 
}

}

我希望这将对您有所帮助!。

您可以使用以下代码来实现挂起的意图

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);
“android.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();
}
}
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")); 
}

}
“android.intent.action.AD”表示AlarmDetails.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();
}
}
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")); 
}

}

我希望这将对您有所帮助。

它需要在当前运行或前屏幕上显示。它需要在当前运行或前屏幕上显示。