gpt4 book ai didi

java - 从一开始就立即调用第二个广播接收器?

转载 作者:太空宇宙 更新时间:2023-11-04 12:05:49 25 4
gpt4 key购买 nike

问题:- 我有两个广播接收器类。第一个在午夜被调用,它获取预定的闹钟时间,当当前时间与商店闹钟时间匹配时,第二个广播接收器被调用。但取而代之的是,当第一个广播接收器被调用时,第二个广播接收器也会在完成第一个广播接收器函数后被调用。

调用第一个广播接收器的函数:

private void setTheTimeToUpdateTables(Context context) {

Log.i("Update table function","Yes");

AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);

Intent alarmIntent=new Intent(context,UpdateTables.class);

PendingIntent pendingIntent=PendingIntent.getBroadcast(context,0,alarmIntent,PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.cancel(pendingIntent);

Calendar alarmStartTime = Calendar.getInstance();

alarmStartTime.setTimeInMillis(System.currentTimeMillis());

alarmStartTime.set(Calendar.HOUR_OF_DAY, 15);
alarmStartTime.set(Calendar.MINUTE, 58);
alarmStartTime.set(Calendar.SECOND, 0);

System.out.println("Updating table time "+alarmStartTime);
System.out.println("Time in millseconds "+alarmStartTime.getTimeInMillis());


alarmManager.setInexactRepeating(AlarmManager.RTC,alarmStartTime.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

Log.d("Alarm","Set for midnight");


}

//第一个广播接收器类

public class UpdateTables extends BroadcastReceiver {

DbHelper dbHelper;
ArrayList<ListMedicine> reminderInfo;

@Override
public void onReceive(Context context, Intent intent) {

dbHelper=new DbHelper(context);
reminderInfo=dbHelper.fetchDataFromReminderinfo();

for (int i=0;i<reminderInfo.size();i++)
{
ListMedicine listMedicine=reminderInfo.get(i);

Calendar calendar=Calendar.getInstance();

System.out.println("Alarm Hour "+listMedicine.getReminder_hour());
System.out.println("Alarm Minutes "+listMedicine.getReminder_min());

calendar.set(Calendar.HOUR_OF_DAY,listMedicine.getReminder_hour());
calendar.set(Calendar.MINUTE, listMedicine.getReminder_min());

setAlarm(calendar,context,i);

}


Log.i("Service Start", CalculateDaysService.TAG);

context.startService(new Intent(context,CalculateDaysService.class));

//Call all the alarm here only.. for next day alarm, call from here.



Log.i("Done","Yes");
}


private void setAlarm(Calendar calNow, Context context, int request_code) {

Log.i("Inside setAlarm,","Yes");

Intent intent = new Intent(context, AlarmReceiver.class);
Long time=calNow.getTimeInMillis();

System.out.println("Reminder Time is "+calNow.get(Calendar.HOUR_OF_DAY)+" "+calNow.get(Calendar.MINUTE));
Log.i("Target",time.toString());

//final int _id = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, request_code, intent, PendingIntent.FLAG_ONE_SHOT);

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);


alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
}
}

//第二个广播接收者

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context k1, Intent k2) {
// TODO Auto-generated method stub

Log.i("Alarm Received","Yes");

Calendar calendar=Calendar.getInstance();

System.out.println("Alarm Receiver call is "+ calendar.getTime() );
Intent i=new Intent(k1,AlertDialogActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
k1.startActivity(i);

}

}

list :-

<receiver android:name=".AlarmReceiver" android:enabled="true"
android:process=":remote"/>

<receiver android:name=".UpdateTables"
android:enabled="true"
android:exported="true">

<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>

</receiver>

最佳答案

据我了解,您想在 alarmStartTime.getTimeInMillis() 时间启动接收器

     Calendar alarmStartTime = Calendar.getInstance();

alarmStartTime.setTimeInMillis(System.currentTimeMillis());

alarmStartTime.set(Calendar.HOUR_OF_DAY, 15);
alarmStartTime.set(Calendar.MINUTE, 58);
alarmStartTime.set(Calendar.SECOND, 0);

System.out.println("Updating table time "+alarmStartTime);
System.out.println("Time in millseconds "+alarmStartTime.getTimeInMillis());

你认为

您可以通过以下方式存储和检索该值:

日历calendar=Calendar.getInstance();

但这是错误的

要存储值,请使用:

SharedPreferences prf = PreferenceManager.getDefaultSharedPreferences(context);

Editor editor = prf.edit();
editor.putLong("SOMESTRINGVALUETOSAVE",alarmStartTime.getTimeInMillis());
editor.commit();

获取:

 SharedPreferences prf = PreferenceManager.getDefaultSharedPreferences(context);
long default_val = System.currentTimeInMillis();// I do not know your default value.
long your_millis = prf.getLong("SOMESTRINGVALUETOSAVE", default_value);

关于java - 从一开始就立即调用第二个广播接收器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40378293/

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