如何在通知已经运行相同的应用程序时给出警告或显示消息

时间:2014-01-21 04:27:38

标签: android notifications

目前,当我按下启动秒表图标时,会启动通知并关闭应用程序。如果再过五分钟后我打开应用程序并按下启动秒表图标,它应显示一条消息,表示已经运行的通知是否要重置。

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String MyText = "Reminder";
Notification mNotification = new Notification(R.drawable.notification_icon, MyText, System.currentTimeMillis() );
//The three parameters are: 1. an icon, 2. a title, 3. time when the notification appears

String MyNotificationTitle = "Practice Time!";
String MyNotificationText  = "";
MyIntent = new Intent(MainActivity.this,Log_Practice_timeActivity.class);
MyIntent.putExtra("s11", "Timer");

Calendar cal = Calendar.getInstance();
MyIntent.putExtra("s1",cal.getTimeInMillis());
DateFormat dateFormat1 = new SimpleDateFormat("HH:mm");
Date date1 = new Date();
MyIntent.putExtra("startTime",dateFormat1.format(date1));

DateFormat dateFormat2 = new SimpleDateFormat("yyyy/MM/dd");
Date date2 = new Date();
MyIntent.putExtra("startDate",dateFormat2.format(date2));
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)(Math.random() * 100),MyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//A PendingIntent will be fired when the notification is clicked. The FLAG_CANCEL_CURRENT flag cancels the pendingintent

mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText,pendingIntent);
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0,   mNotification);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

1 个答案:

答案 0 :(得分:0)

你必须在后台使用Service运行计时器并使用BroadcastReceiver来跟踪进度。只要用户只输入您的应用程序,您就可以执行此操作。您可以参考thisthis