android显示当前打开的程序,android – 在特定时间向用户显示通知,但每当我关闭或打开我的应用程序时它会出现?...

完整的场景是我试图根据用户选择的时间显示通知,我正在使用TimePickerDialog,BroadcastReceiver类和Service类,每个工作正常工作也会在特定时间出现,但问题是当我打开时每次收到通知时关闭应用程序.

Activity.java

Intent myIntent = new Intent(ReminderActivity.this, MyBreakfastReciver.class);

System.out.println("getting Breakfast Reminder");

pendingIntent = PendingIntent.getBroadcast(ReminderActivity.this, 0, myIntent,0);

Calendar calendar = Calendar.getInstance();

calendar.setTimeInMillis(System.currentTimeMillis());

//bTimePart1 and bTimePart2 is the time choosen by user through time picker

calendar.set(Calendar.HOUR_OF_DAY, bTimePart1 );

calendar.set(Calendar.MINUTE, bTimePart2);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

BroadcastReciever

public class MyBreakfastReciver extends BroadcastReceiver

{

@Override

public void onReceive(Context context, Intent intent)

{

Intent service1 = new Intent(context, MyBreakfastAlarmService.class);

context.startService(service1);

}

}

Reciever class

private void showNotification(Context context) {

NotificationCompat.Builder mBuilder =

new NotificationCompat.Builder(context)

.setSmallIcon(R.drawable.notificationlogo)

.setContentTitle("DietGuru")

.setAutoCancel(true)

.setContentText("You haven't logged your BreakFast for today.")

.setSubText("Would you like to do it now?");

// Creates an explicit intent for an Activity in your app

Intent resultIntent = new Intent(context, CalorieMainActivity.class);

// The stack builder object will contain an artificial back stack for the

// started Activity.

// This ensures that navigating backward from the Activity leads out of

// your application to the Home screen.

TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

// Adds the back stack for the Intent (but not the Intent itself)

stackBuilder.addParentStack(CalorieMainActivity.class);

// Adds the Intent that starts the Activity to the top of the stack

stackBuilder.addNextIntent(resultIntent);

resultPendingIntent =

stackBuilder.getPendingIntent(

0,

PendingIntent.FLAG_CANCEL_CURRENT

);

mBuilder.setContentIntent(resultPendingIntent);

//mBuilder.setContentIntent(resultPendingIntent);

mBuilder.setDefaults(Notification.DEFAULT_ALL);

NotificationManager mNotificationManager =

(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

// mId allows you to update the notification later on.

mNotificationManager.notify(1, mBuilder.build());

}

我只想在打开和关闭应用程序时停止通知.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值