gpt4 book ai didi

android - 通知未在特定时间和日期显示

转载 作者:行者123 更新时间:2023-11-30 01:10:15 24 4
gpt4 key购买 nike

我正在开发一款应用程序,可以在特定时间和日期提醒用户特定事件。我已正确设置通知,但它们有时有效,有时无效。

我正在使用 put extra 和 get extra 将点击的位置发送到我的接收器类(将位置从 BuddhaEvent 类发送到 Myreceiver 类)。因此,如果用户单击位置 1,则应从消息类中触发通知 1。我在每个位置调用中都放置了日志语句,请查看。

日志语句的输出是这样的:

位置 0:0
位置 0 : 0
位置 1: 1
位置 0 : 0
位置 2:2

这是显示 4 个通知时的日志语句

POSITION0 : 0

位置 0:0
位置 1: 1
位置 0 : 0
位置 2 : 2
位置 0 : 0
位置 3:3

这里有一个模式,0总是在通知之前被调用!!!

不知道怎么回事。第一个通知工作正常,但之后通知只是随机的。大多数情况下,通知 2 和通知 3 同时到来,有时通知 2 等待通知 3 到来。我注意到的另一个问题是,当我向下滑动通知时,所有通知都有相同的发送时间。我为每个通知设置了不同的 ID。

我在想的是,put extra 和 get extra 做错了。如果有人可以帮助我解决问题,我将不胜感激。

更新:因此,当我单击列表中的第一项时,会显示通知 1。如果我点击第 2 项,则通知 1 和 2 同时出现。当我点击第 3 项时,通知 3 和 1 出现。提前致谢

更新 2:程序每次都调用 0,我决定将 PUT POSITION0 更改为不同的东西,现在一次只显示一个通知(这仅在时间过去时有效)但是当我尝试安排通知时事件相同的问题退出(例如同时显示两个通知)。

这是我的主类`public class BuddhaEvent extends AppCompatActivity implements View.OnClickListener { 公共(public)列表 myList = new ArrayList(); android.support.v4.app.NotificationCompat.Builder通知;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_buddhism_event);


fill_eventsList();
fill_ListView();
clickResponse();

Button buddah_button_event = (Button) findViewById(R.id.button_event_back_buddhism);
buddah_button_event.setOnClickListener(this);


}

private void fill_eventsList() {
myList.add(new list("Jan-24", R.drawable.chris, "Mahayana countries the new year starts on the first full moon day in January.", "Mahayana New Year"));
myList.add(new list("Feb-2", R.drawable.chris, "Chinese festival celebrated at the turn of the traditional lunisolar Chinese calendar.", "Chinese New Year"));
myList.add(new list("Feb-15", R.drawable.chris, "Celebrates the day when the Buddha is said to have achieved Parinirvana, or complete Nirvana", "Nirvana Day"));
myList.add(new list("Mar-23", R.drawable.chris, "Celebration in honour of the Sangha, or the Buddhist community", "Magha Puja Day"));
myList.add(new list("Apr-22", R.drawable.chris, "Buddhist New Year", "Theravada New Year"));
myList.add(new list("May-15", R.drawable.chris, "Celebrates the Buddha's birthday", "Wesak - Buddha Day"));
myList.add(new list("Jul-11", R.drawable.chris, "Honor the spirits of one's ancestors.", "Obon"));
myList.add(new list("Jul-19", R.drawable.chris, "Celebrates Buddha's teachings of peace and enlightenment", "Asala - Dharma Day"));
myList.add(new list("Dec-8", R.drawable.chris, "The day that the historical Buddha, Siddhartha Gautama (Shakyamuni), experienced enlightenment", "Bodhi Day"));

}

private void fill_ListView() {
ArrayAdapter<list> listArrayAdapter = new myListAdapter();
ListView list = (ListView) findViewById(R.id.list_eventsView);
list.setAdapter(listArrayAdapter);

}

public void clickResponse() {
ListView l = (ListView) findViewById(R.id.list_eventsView);
if (l != null) {
l.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, final View viewClicked,
final int position, long id) {


AlertDialog.Builder alert = new AlertDialog.Builder(
BuddhaEvent.this);
alert.setTitle("Reminder!!");
alert.setMessage("Do you want to be reminded of this event in future?");
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("OUTSIDE", "CHECKING WHICH IS CLICKED : " + which);

if (position == 0) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, 6);
calendar.set(Calendar.DAY_OF_MONTH, 20);
calendar.set(Calendar.HOUR_OF_DAY, 21);
calendar.set(Calendar.MINUTE, 2);
calendar.set(Calendar.SECOND, 0);

Intent intent = new Intent(getApplicationContext(), MyReceiver.class);
intent.putExtra("position0", position);


PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

}

if (position == 1) {
Calendar calendar2 = Calendar.getInstance();

calendar2.set(Calendar.MONTH, 6);
calendar2.set(Calendar.DAY_OF_MONTH, 20);

calendar2.set(Calendar.HOUR_OF_DAY, 21);
calendar2.set(Calendar.MINUTE, 3);
calendar2.set(Calendar.SECOND, 0);


Intent intent2 = new Intent(getApplicationContext(), MyReceiver.class);
intent2.putExtra("position1",position );



PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getApplicationContext(), 101, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager2.set(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), pendingIntent2);
}

if (position == 2) {

Calendar calendar3 = Calendar.getInstance();

calendar3.set(Calendar.MONTH, 6);
calendar3.set(Calendar.DAY_OF_MONTH, 20);

calendar3.set(Calendar.HOUR_OF_DAY, 21);
calendar3.set(Calendar.MINUTE, 4);
calendar3.set(Calendar.SECOND, 0);


Intent intent3 = new Intent(getApplicationContext(), MyReceiver.class);
intent3.putExtra("position2", position);


PendingIntent pendingIntent3 = PendingIntent.getBroadcast(getApplicationContext(), 105, intent3, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar3.getTimeInMillis(), pendingIntent3);
}


if (position == 3) {

Calendar calendar4 = Calendar.getInstance();

calendar4.set(Calendar.MONTH, 6);
calendar4.set(Calendar.DAY_OF_MONTH, 20);

calendar4.set(Calendar.HOUR_OF_DAY, 21);
calendar4.set(Calendar.MINUTE, 5);
calendar4.set(Calendar.SECOND, 0);


Intent intent4 = new Intent(getApplicationContext(), MyReceiver.class);
intent4.putExtra("position3", position);


PendingIntent pendingIntent4 = PendingIntent.getBroadcast(getApplicationContext(), 102, intent4, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager4 = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager4.set(AlarmManager.RTC, calendar4.getTimeInMillis(), pendingIntent4);
}
dialog.dismiss();
}
});

alert.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();
}
});
alert.show();
}
});
}
}


private class myListAdapter extends ArrayAdapter<list> {
public myListAdapter() {
super(BuddhaEvent.this, R.layout.custom_events_layout, myList);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.custom_events_layout, parent, false);
}
list current_list = myList.get(position);


ImageView imageView = (ImageView) itemView.findViewById(R.id.id_image);
imageView.setImageResource(current_list.getIconId());


TextView titleTxt = (TextView) itemView.findViewById(R.id.id_event);
titleTxt.setText(current_list.getName());

EditText text = (EditText) itemView.findViewById(R.id.id_desp);
text.setText(current_list.getDetails());


TextView date = (TextView) itemView.findViewById(R.id.id_date);
date.setText(current_list.getDate());

return itemView;
}


}


public void onClick(View v) {
startActivity(new Intent(BuddhaEvent.this, BuddhaScreen.class));
}

}`这是扩展广播接收器的 MyReceiver 类

public class MyReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();


if(bundle==null){
return;
}else{
if(bundle.getInt("position0")==0){
Log.d("INSIDE", "POSITION0 : " + bundle.getInt("position0"));

Utils utils = new Utils();
utils.generateNotification(context);
}

if(bundle.getInt("position1")==1){
Log.d("INSIDE", "POSITION1: " + bundle.getInt("position1"));

Utils utils = new Utils();
utils.generateNotification2(context);
}

if(bundle.getInt("position2")==2){
Log.d("INSIDE", "POSITION2 : " + bundle.getInt("position2"));

Utils utils = new Utils();
utils.generateNotification3(context);
}

if(bundle.getInt("position3")==3){
Log.d("INSIDE", "POSITION3 : " + bundle.getInt("position3"));

Utils utils = new Utils();
utils.generateNotification4(context);
}
}


}

这是我的 utils 类,用于跟踪通知

public class Utils {


public void generateNotification(Context context) {

NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent repeating_intent = new Intent(context, BuddhaNow.class);
// replace the old activity if there is a case that it is already opened
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);


NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentTitle("Notification Title1")
.setContentText("Notification Text1")
.setAutoCancel(true);

notificationManager.notify(100, builder.build());


}


public void generateNotification2(Context context) {

NotificationManager notificationManager2 = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent repeating_intent2 = new Intent(context, BuddhaNow.class);
// replace the old activity if there is a case that it is already opened
repeating_intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 101, repeating_intent2, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder2 = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent2)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())

.setContentTitle("Notification Title2")
.setContentText("Notification Text2")
.setAutoCancel(true);

notificationManager2.notify(101, builder2.build());


}

public void generateNotification3(Context context) {

NotificationManager notificationManager3 = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent repeating_intent3 = new Intent(context, BuddhaNow.class);
// replace the old activity if there is a case that it is already opened
repeating_intent3.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent3 = PendingIntent.getActivity(context, 102, repeating_intent3, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder3 = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent3)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())

.setContentTitle("Notification Title3")
.setContentText("Notification Text3")
.setAutoCancel(true);

notificationManager3.notify(102, builder3.build());


}

public void generateNotification4(Context context) {

NotificationManager notificationManager4 = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent repeating_intent4 = new Intent(context, BuddhaNow.class);
// replace the old activity if there is a case that it is already opened
repeating_intent4.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent4 = PendingIntent.getActivity(context, 105, repeating_intent4, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder4 = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent4)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())

.setContentTitle("Notification Title4")
.setContentText("Notification Text4")
.setAutoCancel(true);

notificationManager4.notify(105, builder4.build());


}

}

最佳答案

你应该使用 setExact方法以存档警报触发的确切时间。

但主要的问题是当没有 position0bundle.getInt("position0")==0 将始终为 true在 bundle 中,因为:

Returns the value associated with the given key, or 0 if no mapping of the desired type exists for the given key.

因此,为了解决这个问题,传递位置:

intent.putExtra("position", position);

然后在广播接收器中检查它:

int position = bundle.getInt("position");

switch (position):
case 0:
utils.generateNotification(context);
break;
case 1:
utils.generateNotification1(context);
break;
...

关于android - 通知未在特定时间和日期显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38493360/

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