gpt4 book ai didi

java - 为什么我的推送通知不断不规律地重复?

转载 作者:行者123 更新时间:2023-12-02 11:59:38 24 4
gpt4 key购买 nike

使用警报管理器和待处理 Intent 设置的推送通知显示异常行为。由于某种原因,在我打开应用程序后一分钟或更短的时间内就会出现通知。有时一分钟不止一次,然后一段时间就不会出现了。我的想法是每天发出一次通知,假设用户在 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());
}

}

以及我的 list :

<receiver android:name=".Receiver"></receiver>

有人知道为什么会出现这个问题吗?

非常感谢,谢谢。

最佳答案

第二个参数为AlarmManager.setRepeating method 是闹钟首次响起的时间。

您将使用以下参数调用此方法:

alarm.setRepeating(alarm.RTC_WAKEUP, System.currentTimeMillis(), alarm.INTERVAL_DAY, pendingIntent);

这意味着您将重复闹钟设置为在调用该方法后立即响起(当创建 Main_Menu Activity 时)。

根据您的描述,您似乎正在寻找类似的东西:

alarm.setRepeating(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis() + AlarmManager.INTERVAL_DAY), AlarmManager.INTERVAL_DAY, pendingIntent);

关于java - 为什么我的推送通知不断不规律地重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47361159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com