*我开发一个Android应用程序触发在用户选择一定的时间报警。但问题是,如果用户从现在设置的时间一点点量报警时才会触发。因此,这就像如果应用程序没有运行的报警不会触发。我已经获得了锁,还设置了标志,以保持在屏幕上。有时候,当我把它设置为15分钟,从现在或类似的东西,它的工作原理,然后我再试一次,它没有。我还包含在清单中的广播接收机。这里是我的日程表报警code:
*I am developing an Android application which triggers an alarm at certain time that the user selects. But the problem is this alarm is only triggered if the user sets it to a little amount of time from now. So it's like if the app is not running the alarm won't trigger. I already acquired a lock and also set the flags to keep the screen on. Sometimes when I set it to 15 minutes from now or something like that it works and then I try again and it doesn't. I also included the broadcast receiver in the manifest. Here's my schedule alarm code:
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,时间是用户输入的时间字符串:
To get the calendarTime i use the following function where time is the string of the time the user entered:
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; }最后,这是我的广播接收器:
And finally this is my broadcast receiver:
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); } }我想AP preciate什么它可能是触发只是有时报警的原因任何帮助。
I would appreciate any help in what it could be the reason for the alarms triggering only sometimes.
推荐答案这code是我工作的罚款,你可以用它我希望它会为你工作。
this code is working fine for me you can use it i hope it will work for you.
这是主activty一个获取数据的方法。
this is a fetch data method in main activty
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); } }更多推荐
警报并不总是触发的Android
发布评论