通知说自己打开 [英] Notifications that open by themselves
问题描述
我的应用程序,而不超过code,我写了一个逻辑意义上开放的通知。我应该在每天的同一时间收到通知,但如果我打开应用程序,我得到了相同的通知。这是code在MainActivity:
公共无效setRepeatingAlarm(){
意向意图=新的意图(这一点,MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(此,0,
意图,PendingIntent.FLAG_CANCEL_CURRENT);
台历挂历= Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,13);
calendar.set(Calendar.MINUTE,00);
calendar.set(Calendar.SECOND,00);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),24 * 60 * 60 * 1000,pendingIntent);
}
这是MyAlarmService:
公共类MyAlarmService扩展的BroadcastReceiver {
NotificationManager处;
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
纳米=(NotificationManager)上下文
.getSystemService(Context.NOTIFICATION_SERVICE);
从CharSequence的=Locali都灵;
CharSequence的消息=VISITA乐serate!;
意图动作=新的意图(背景下,MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,
动作,0);
注意notif =新的通知(R.drawable.disco,
VISITA乐serate!,System.currentTimeMillis的());
notif.setLatestEventInfo(上下文,从,消息,contentIntent);
notif.flags | = Notification.FLAG_AUTO_CANCEL;
nm.notify(0,notif);
}
}
为什么我得到通知,当我打开应用程序或至少不只是在设定的时间?
修改
MyAlarmService:
公共类MyAlarmService扩展的BroadcastReceiver {
NotificationManager处;
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
纳米=(NotificationManager)上下文
.getSystemService(Context.NOTIFICATION_SERVICE);
从CharSequence的=Locali都灵;
CharSequence的消息=VISITA乐serate!;
意图动作=新的意图(背景下,MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,
动作,0);
注意notif =新的通知(R.drawable.disco,
VISITA乐serate!,System.currentTimeMillis的());
notif.setLatestEventInfo(上下文,从,消息,contentIntent);
notif.flags | = Notification.FLAG_AUTO_CANCEL;
nm.notify(0,notif);
共享preferences米preF = context.getShared preferences(pref_name,Context.MODE_PRIVATE);
共享preferences.Editor mEditor = M pref.edit();
很长一段时间= System.currentTimeMillis的();
mEditor.putLong(UPDATE_TIME,时间);
mEditor.commit();
}这是MainActivity:
公共无效setRepeatingAlarm(){
意向意图=新的意图(这一点,MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(此,0,
意图,PendingIntent.FLAG_CANCEL_CURRENT);
长startAt;
长周期;
共享preferences米preF = context.getShared preferences(pref_name,Context.MODE_PRIVATE);
长DIF = System.currentTimeMillis的() - 米pref.getLong(UPDATE_TIME,0);
如果(DIF> = UPDATE_PERIOD){
startAt = 0;
周期= UPDATE_PERIOD;
} 其他 {
startAt = DIF;
周期= DIF;
}
am.setRepeating(AlarmManager.RTC_WAKEUP,startAt,期间,pendingIntent);
}
其实它的工作原理完全一样code被写入。这种行为的原因是在你的 startAt
参数 setRepeating
方法。
am.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
24 * 60 * 60 * 1000,
pendingIntent);
calendar.getTimeInMillis()
时间以毫秒为单位的报警器应首先熄灭,使用适当的时钟(取决于报警类型)。
例如:
// 1秒后发出第一警报和重复每24小时
am.setRepeating(AlarmManager.RTC_WAKEUP,
1000,
24 * 60 * 60 * 1000,
pendingIntent);
//经过10第二个发送第一警报和重复每24小时
am.setRepeating(AlarmManager.RTC_WAKEUP,
10000,
24 * 60 * 60 * 1000,
pendingIntent);
更新:
您需要保存你的最后更新时间:
公共类MyAlarmService扩展的BroadcastReceiver {
NotificationManager处;
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
//这里发送报警
// ...
共享preferences米preF = context.getShared preferences(pref_name,Context.MODE_PRIVATE);
共享preferences.Editor mEditor = M pref.edit();
mEditor.putLong(UPDATE_TIME,时间);
mEditor.commit();
}
}
然后你可以让你的 setRepeatingAlarm
的方法是这样的:
公共无效setRepeatingAlarm(){
意向意图=新的意图(这一点,MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(此,0,
意图,PendingIntent.FLAG_CANCEL_CURRENT);
长startAt;
长周期;
共享preferences米preF = context.getShared preferences(pref_name,Context.MODE_PRIVATE);
长DIF = System.currentTimeMillis的() - 米pref.getLong(UPDATE_TIME,0);
如果(DIF> = UPDATE_PERIOD){
startAt = 0;
周期= UPDATE_PERIOD;
} 其他 {
startAt = DIF;
周期= DIF;
}
am.setRepeating(
AlarmManager.RTC_WAKEUP,
startAt,
期,
pendingIntent);
}
the notifications of my app open without a logical sense than the code that I wrote. I should receive a notification every day at the same time but if I open the app I get the same notification. This is the code in MainActivity:
public void setRepeatingAlarm() {
Intent intent = new Intent(this, MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),24*60*60*1000,pendingIntent);
}
And this is MyAlarmService:
public class MyAlarmService extends BroadcastReceiver {
NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Locali Torino";
CharSequence message = "Visita le serate!";
Intent action = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
action, 0);
Notification notif = new Notification(R.drawable.disco,
"Visita le serate!", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
notif.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(0, notif);
}
}
Why I get notifications when I open the app or at least not only at the set time?
EDIT
MyAlarmService:
public class MyAlarmService extends BroadcastReceiver {
NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Locali Torino";
CharSequence message = "Visita le serate!";
Intent action = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
action, 0);
Notification notif = new Notification(R.drawable.disco,
"Visita le serate!", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
notif.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(0, notif);
SharedPreferences mPref = context.getSharedPreferences("pref_name", Context.MODE_PRIVATE);
SharedPreferences.Editor mEditor = mPref.edit();
long time = System.currentTimeMillis();
mEditor.putLong("UPDATE_TIME", time);
mEditor.commit();
}And this is MainActivity:
public void setRepeatingAlarm() {
Intent intent = new Intent(this, MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
long startAt;
long period;
SharedPreferences mPref = context.getSharedPreferences("pref_name", Context.MODE_PRIVATE);
long dif = System.currentTimeMillis() - mPref.getLong("UPDATE_TIME", 0);
if (dif >= UPDATE_PERIOD) {
startAt = 0;
period = UPDATE_PERIOD;
} else {
startAt = dif;
period = dif;
}
am.setRepeating(AlarmManager.RTC_WAKEUP, startAt, period,pendingIntent);
}
Actually it works exactly as code is written. The cause of that behavior is your startAt
parameter in setRepeating
method.
am.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
24*60*60*1000,
pendingIntent);
calendar.getTimeInMillis()
time in milliseconds that the alarm should first go off, using the appropriate clock (depending on the alarm type).
For example :
//send first alarm after 1 second and repeat it every 24 hours
am.setRepeating(AlarmManager.RTC_WAKEUP,
1000,
24*60*60*1000,
pendingIntent);
//send first alarm after 10 second and repeat it every 24 hours
am.setRepeating(AlarmManager.RTC_WAKEUP,
10000,
24*60*60*1000,
pendingIntent);
UPDATE :
You need to save your last update time :
public class MyAlarmService extends BroadcastReceiver {
NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
//send alarm here
//...
SharedPreferences mPref = context.getSharedPreferences("pref_name", Context.MODE_PRIVATE);
SharedPreferences.Editor mEditor = mPref.edit();
mEditor.putLong("UPDATE_TIME", time);
mEditor.commit();
}
}
Then you can make your setRepeatingAlarm
method like this :
public void setRepeatingAlarm() {
Intent intent = new Intent(this, MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
long startAt;
long period;
SharedPreferences mPref = context.getSharedPreferences("pref_name", Context.MODE_PRIVATE);
long dif = System.currentTimeMillis() - mPref.getLong("UPDATE_TIME", 0);
if (dif >= UPDATE_PERIOD) {
startAt = 0;
period = UPDATE_PERIOD;
} else {
startAt = dif;
period = dif;
}
am.setRepeating(
AlarmManager.RTC_WAKEUP,
startAt,
period,
pendingIntent);
}
这篇关于通知说自己打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!