在安卓中重复警报通知



这是我在创建活动类时设置每日警报服务的代码。

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int curHr = calendar.get(Calendar.HOUR_OF_DAY);
if (curHr >=9) {
// Since current hour is over 9, setting the date to the next day
calendar.add(Calendar.DATE, 1);
}
calendar.set(Calendar.HOUR_OF_DAY, aHOUR);
calendar.set(Calendar.MINUTE, aMINUTE);
calendar.set(Calendar.SECOND, aSECOND);
Intent intent1 = new Intent(getBaseContext(), MyReciever.class);
intent1.putExtra("requestcode", "0");
intent1.putExtra("status", "1");
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), INTERVAL, pendingIntent);

这是我在接收机上的代码

String req=intent.getStringExtra("requestcode");
String state=intent.getStringExtra("status");
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, FirstActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, Integer.valueOf(req),
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(Integer.valueOf(req)==1)
{
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Hi "+appref.getUserFirstName()+" "+appref.getUserLastName())
.setContentText("Having a great Day? Log it in MyApp").setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
if(Integer.valueOf(state)==1)
{
notificationManager.notify(MID1, mNotifyBuilder.build());
}
else
{
notificationManager.cancel(MID1);
}

除了重复之外,一切正常。问题是alarm notification第二天不工作。如果您有解决此问题的任何解决方案,请提供帮助。

INTERVALalarmmanager.intervalday_同样的问题,然后更改为int interval=86000L但问题看起来是一样的。

提前致谢

试试这个。它对我有用。

Intent myIntent = new Intent(this, Receiver.class);
myIntent.putExtra("key", "Alert");
pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, myIntent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar=Calendar.getInstance();
// Calendar.set(int year, int month, int day, int hourOfDay, int minute, int second)
calendar.set(2013, Calendar.OCTOBER, 10, 12, 10, 20);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);

此处参数 10*1000 = 10 秒将重复报警

接收器.java

public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,intent.getStringExtra("key"),Toast.LENGTH_SHORT).show();
}
}

相关内容

  • 没有找到相关文章

最新更新