使用警报管理器和待处理意图设置的推送通知显示异常行为。出于某种原因,在我打开应用程序后,通知会在一分钟或更短的时间内出现。有时不止一分钟,然后它不会出现一段时间。我的想法是每天发出一次通知,假设用户在24小时内没有访问我的应用程序。我打算取消任何以前的计时器,并在用户下次访问我的应用程序时开始新计时器1天。以下是我的代码。
public class Main_Menu extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Button goToGame1;
goToGame1 = (Button) findViewById(R.id.Game1);
goToGame1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent beAtGame1 = new Intent(Main_Menu.this, memory_modes.class);
startActivity(beAtGame1);
}
});
Button goToGame2;
goToGame2 = (Button) findViewById(R.id.Game2);
goToGame2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent beAtGame2 = new Intent(Main_Menu.this, computation_modes.class);
startActivity(beAtGame2);
}
});
Button goToGame3;
goToGame3 = (Button) findViewById(R.id.Game3);
goToGame3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent beAtGame3 = new Intent(Main_Menu.this, evaluation_modes.class);
startActivity(beAtGame3);
}
});
Button goToGame4;
goToGame4 = (Button) findViewById(R.id.Game4);
goToGame4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent beAtGame4 = new Intent(Main_Menu.this, attention_modes.class);
startActivity(beAtGame4);
}
});
Button goToTrackers;
goToTrackers = (Button) findViewById(R.id.Trackers);
goToTrackers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent beAtTrackers = new Intent(Main_Menu.this,trackerMenu.class);
startActivity(beAtTrackers);
}
});
Intent intent = new Intent(Main_Menu.this, Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Main_Menu.this, 1, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
alarm.cancel(pendingIntent);
alarm.setRepeating(alarm.RTC_WAKEUP, System.currentTimeMillis(), alarm.INTERVAL_DAY, pendingIntent);
}
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
和
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
showNotification(context);
}
public void showNotification(Context context) {
Intent intent = new Intent(context, Main_Menu.class);
PendingIntent pi = PendingIntent.getActivity(context, 1, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.smalliconfinal)
.setColor(Color.argb(000,255,177,17))
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon4))
.setContentTitle("Title")
.setContentText("Some text.");
mBuilder.setLights(0xffffb111, 750, 750);
mBuilder.setContentIntent(pi);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
和我的清单:
<receiver android:name=".Receiver"></receiver>
有人知道为什么会出现这个问题吗?
非常感谢, 谢谢。
答案 0 :(得分:1)
AlarmManager.setRepeating方法的第二个参数是闹钟应该首先关闭的时间。
您正在使用以下参数调用此方法:
alarm.setRepeating(alarm.RTC_WAKEUP, System.currentTimeMillis(), alarm.INTERVAL_DAY, pendingIntent);
这意味着您只要在调用方法时(设置Main_Menu
活动时)就将重复闹钟设置为关闭。
根据您的描述,您似乎正在寻找类似的内容:
alarm.setRepeating(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis() + AlarmManager.INTERVAL_DAY), AlarmManager.INTERVAL_DAY, pendingIntent);