我知道这个问题已被多次询问,我已经实施了所提供的解决方案。但是,当我一次又一次地重复启动应用程序时,我会多次收到通知。它将在24小时后收到通知。请帮忙。
在onCreate我调用下面的函数:
private void setDailyCoins() {
Intent intent1 = new Intent(HomeActivity.this,AlarmReceiver.class);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getBroadcast(HomeActivity.this, 0, intent1, 0);
am.cancel(pendingIntent);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE,02);
calendar.set(Calendar.SECOND, 02);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
24*60*60*1000, pendingIntent);
}
并在收到通知时:
public class AlarmReceiver extends BroadcastReceiver {
int MID;
private ParseQuery<ParseObject> query;
@Override
public void onReceive(Context context, Intent intent) {
long when = System.currentTimeMillis();
System.out.println("noti " + when);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context,
com.socialcurrency.teenpatti.LoginActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context)
.setSmallIcon(R.drawable.teen_patti_icon)
.setContentTitle("ppp")
.setContentText(
"Please Collect 10,000 coins daily reward. Enjoy!!!")
.setSound(alarmSound).setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
notificationManager.notify(MID, mNotifyBuilder.build());
MID++;
calltoAddCoins(context);
}
在Manifest中添加了接收器
<receiver android:name=".AlarmReceiver" />