*我正在开发一个Android应用程序,它会在用户选择的某个时间触发警报。但问题是,只有当用户将其设置为从现在开始的一段时间后才会触发此警报。因此,如果应用程序没有运行警报就不会触发。我已经获得了一个锁,并设置了标志以保持屏幕开启。有时当我把它设置为15分钟后或类似的东西它起作用然后我再试一次它并没有。我还在清单中包括广播接收器。这是我的日程警报代码:
public void schedule(Activity activity){
Context context = activity.getApplicationContext();
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmActivityBroadcastReceiver.class);
intent.putExtra("id", this.getId());
pendingIntent = PendingIntent.getBroadcast(context, 1234565666, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, this.getCalendarTime().getTimeInMillis(), pendingIntent);
}
要获取calendarTime,我使用以下函数,其中time是用户输入的时间字符串:
private Calendar convertTimeStringToCalendar(){
String[] timeArray = this.time.split(":");
int minute = Integer.parseInt(timeArray[1]);
int hour = Integer.parseInt(timeArray[0]);
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
int nowHour = c.get(Calendar.HOUR_OF_DAY);
int nowMinute = c.get(Calendar.MINUTE);
if (hour < nowHour || (hour == nowHour && minute <= nowMinute)) {
c.add(Calendar.DAY_OF_YEAR, 1);
}
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c;
}
最后这是我的广播接收器:
public class AlarmActivityBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
WakeLock.acquire(context);
Alarm alarm = DataBaseSQL.getAlarm(intent.getLongExtra("id", 0L));
Intent alarmActivityIntent = new Intent(context, AlarmActivity.class);
alarmActivityIntent.putExtra("id", alarm.getId());
alarmActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
alarmActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
context.startActivity(alarmActivityIntent);
}
}
我很感激任何帮助,因为它可能是警报有时触发的原因。
答案 0 :(得分:0)
这段代码对我来说很好,你可以使用它我希望它对你有用。
这是主要活动中的获取数据方法
public void fetchdata() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
Intent myIntent = new Intent(MainActivity.this, MyBroadcastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}
This is broadcast receiver class
public class MyBroadcastReceiver extends BroadcastReceiver {
int notifyID=1;
public void onReceive(Context context, Intent intent) {
Intent i=new Intent();
PendingIntent pi=PendingIntent.getBroadcast(context,0,i,0);
NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification(R.drawable.ic_launcher,"Notification Triggered",System.currentTimeMillis());
CharSequence from="Remainder";
CharSequence message="Hurry up!!!!";
notify.setLatestEventInfo(context, from, message, pi);
nm.notify(notifyID,notify);
// Vibrate the mobile phone
Vibrator vibrator = (Vibrator) context
.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
}
}