问题: - 我有两个广播接收器类。一个在午夜调用,它获取预定的闹钟时间,当当前时间与商店闹钟时间匹配时,调用第二个广播接收器。但是,不是这样,在第一个广播接收器被调用时,第二个广播接收器也被调用。
呼叫第一个广播接收器的功能:
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);
}
}
清单: -
<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>
答案 0 :(得分:0)
据我了解,您想在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=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);