Android 设置报警后会立即显示报警通知,即使时间是未来时间
我在设置报警时遇到问题,我将给alarmManager.Set方法提供未来的时间,但一旦触发报警,我就会收到通知,而不是在指定的时间收到通知。请让我知道我犯了什么错误。这是我第一次使用闹钟,请告诉我我的错误。欢迎所有建议。提前谢谢 我的报警设置方法: 我正在获取用户输入的时间,格式化时间并将其提供给alarmManager。设置方法:Android 设置报警后会立即显示报警通知,即使时间是未来时间,android,alarmmanager,android-notifications,Android,Alarmmanager,Android Notifications,我在设置报警时遇到问题,我将给alarmManager.Set方法提供未来的时间,但一旦触发报警,我就会收到通知,而不是在指定的时间收到通知。请让我知道我犯了什么错误。这是我第一次使用闹钟,请告诉我我的错误。欢迎所有建议。提前谢谢 我的报警设置方法: 我正在获取用户输入的时间,格式化时间并将其提供给alarmManager。设置方法: String givenDateString = dateTime; Log.d("Pana", "The value of Date Time is "
String givenDateString = dateTime;
Log.d("Pana", "The value of Date Time is " +dateTime);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("GMT+0530"));
try {
Date mDate = sdf.parse(givenDateString);
timeInMilliseconds = mDate.getTime();
Log.d("Pana ", "Date in milli :: " + timeInMilliseconds);
} catch (ParseException e) {
e.printStackTrace();
}
// create an Intent and set the class which will execute when Alarm triggers, here we have
// given AlarmReciever in the Intent, the onRecieve() method of this class will execute when
// alarm triggers and
//we will write the code to send SMS inside onRecieve() method pf Alarmreciever class
Intent intentAlarm = new Intent(this, AlarmReceiverNotification.class);
// create the object
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMilliseconds , PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Toast.makeText(this, "Alarm Scheduled for Tomorrow", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, MyAlarmService.class);
intent.putExtra("Time", timeInMilliseconds);
startService(intent);
boolean alarmUp = (PendingIntent.getBroadcast(getApplicationContext(), 0,
new Intent("com.ms.t.tms.MY_UNIQUE_ACTION"),
PendingIntent.FLAG_NO_CREATE) != null);
if (alarmUp)
{
Log.d("myTag", "Alarm is already active");
}
我的接受者类别:
public class MyAlarmService extends IntentService {
private NotificationManager mManager;
Notification notification;
Intent intent;
public MyAlarmService() {
super("MyAlarmService");
}
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public MyAlarmService(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d("Pana", "Alarm is triggered");
Long time = intent.getLongExtra("Time", 0);
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
notification = new Notification(R.mipmap.ic_launcher,"This is a test message!", time);
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);
mManager.notify(0, notification);
}
}
绑定服务时会立即显示通知
MyAlarmService
,因为您在onStartCommand()
方法中显示通知
您应该将
日历
对象传递到MyAlarmService
,以便在特定时间显示通知。您可以参考您的通知在绑定服务MyAlarmService
时立即显示,因为您是在onStartCommand()
方法中显示通知的
您应该将
日历
对象传递到MyAlarmService
,以便在特定时间显示通知。您可以参考以下是使用报警管理器类设置报警的代码。请记住,您的唯一id应该不同,时间应该以毫秒为单位
int uniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
AlarmManager mAlarm = (AlarmManager) MainActivity.this
.getSystemService(MainActivity.this.ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
// Unique id should be different all the time. So I used it with System current Millis seconds.
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,
uniqueId, intentR, 0);
// Give you time in milliseconds for the Alarm. I given Alarm time after 1 minute (1 * 60 * 10000
mAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, systemTime + 1 * 60 * 60 * 1000,
pendingIntent);
intentArray.add(pendingIntent);
int uniqueId=(int)(System.currentTimeMillis()&0xfffffff);
AlarmManager mAlarm=(AlarmManager)MainActivity.this
.getSystemService(MainActivity.this.ALARM\u服务);
ArrayList intentArray=新的ArrayList();
//唯一id应始终不同。所以我用它来表示系统电流毫秒。
PendingEvent PendingEvent=PendingEvent.getBroadcast(MainActivity.this,
唯一ID,intentR,0);
//以毫秒为单位给出报警时间。我给出了1分钟后的报警时间(1*60*10000
mAlarm.set(AlarmManager.Appeased\u REALTIME\u唤醒,系统时间+1*60*60*1000,
悬挂式);
intentArray.add(挂起内容);
以下是使用Alarm Manager类设置报警的代码。请记住,您的唯一id应该不同,时间应该以毫秒为单位
int uniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
AlarmManager mAlarm = (AlarmManager) MainActivity.this
.getSystemService(MainActivity.this.ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
// Unique id should be different all the time. So I used it with System current Millis seconds.
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,
uniqueId, intentR, 0);
// Give you time in milliseconds for the Alarm. I given Alarm time after 1 minute (1 * 60 * 10000
mAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, systemTime + 1 * 60 * 60 * 1000,
pendingIntent);
intentArray.add(pendingIntent);
int uniqueId=(int)(System.currentTimeMillis()&0xfffffff);
AlarmManager mAlarm=(AlarmManager)MainActivity.this
.getSystemService(MainActivity.this.ALARM\u服务);
ArrayList intentArray=新的ArrayList();
//唯一id应该一直是不同的,所以我用它来表示系统当前的毫秒数。
PendingEvent PendingEvent=PendingEvent.getBroadcast(MainActivity.this,
唯一ID,intentR,0);
//以毫秒为单位给出报警时间。我在1分钟后给出了报警时间(1*60*10000)
mAlarm.set(AlarmManager.Appeased\u REALTIME\u唤醒,系统时间+1*60*60*1000,
悬挂式);
intentArray.add(挂起内容);
面临同样的问题,但最终能够解决
在我的例子中,使用SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy HH:mm", Locale.ENGLISH);
您可以检查您的timeinmillizes
是否正确或未使用
Log.i(TAG, "Date in milli :: " + timeInMilliseconds);
Log.i(TAG,"System.currentTimeMillis() = "+System.currentTimeMillis());
如果timeinmillides
小于System.currentTimeMillis()
则timeinmillides
是错误的,否则是正确的
在这种情况下,M是一年中的月份从1开始到12
在我的情况下,我提供的月份从0到11
为了解决这个问题,我将月数增加了1,然后用SimpleDateFormat
解析,问题就解决了。面对同样的问题,但最终能够解决它
在我的例子中,使用SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy HH:mm", Locale.ENGLISH);
您可以检查您的timeinmillizes
是否正确或未使用
Log.i(TAG, "Date in milli :: " + timeInMilliseconds);
Log.i(TAG,"System.currentTimeMillis() = "+System.currentTimeMillis());
如果timeinmillides
小于System.currentTimeMillis()
则timeinmillides
是错误的,否则是正确的
在这种情况下,M是一年中的月份从1开始到12
在我的情况下,我提供的月份从0到11
为了解决这个问题,我将月份增加了1,然后用SimpleDateFormat
解析,问题就解决了。你确定没有将时间设置为过去吗?timeinmillides
可能不正确,请正确检查。是的。我已经使用联机日期到毫秒转换器和毫秒到日期转换器进行了检查。还有一件事是如果在“通知显示”中为明天设置了日期,则应用程序会显示明天的日期,但通知会立即显示。我已经检查过了。谷歌日历应用程序也会显示即时通知?您确定没有将时间设置为过去吗?timeinmillides
可能不正确,请正确检查。是..我已经检查过了使用在线日期到毫秒转换器和毫秒到日期转换器进行编辑。还有一件事是,如果在通知中设置了明天的日期,则显示的应用程序将显示明天的日期,但通知会立即显示。我已检查过它。谷歌日历应用程序也会显示即时通知?好的,我觉得这会对我有所帮助。我在哪里可以显示notif除了onStart()命令之外的其他检查?例如,在调用mManager.NOTIFY(0,notification)之前,您应该添加类似(intent.getBooleanExtra(intent\u NOTIFY,false))
的检查;
为了使通知不会在您在引用的链接中绑定服务时立即出现,通知方法仅在onStartCommand()方法中正确调用..我已经这样做了,报警通知不会在设置后立即显示,即使在设置的时间内也不会显示