我正试图在每天晚上7点播放铃声,但是在其未决意图正在注册广播后立即播放铃声。
我通过按钮点击在前台调用服务,并在onStartCommand中创建了待处理的意图:
@Override public int onStartCommand(Intent intent, int flags, int startId) { startForeground(FOREGROUND_ID, buildForegroundNotification("DummyApp")); c = Calendar.getInstance(); AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); int interval = 1000 * 60 * 60*24; c.setTimeInMillis(System.currentTimeMillis()); c.set(Calendar.HOUR, 19); c.set(Calendar.MINUTE,00); manager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), interval, pendingIntent); Intent alarmIntent = new Intent(AlarmService.this, DataProcessor.class); pendingIntent = PendingIntent.getBroadcast(AlarmService.this, 0, alarmIntent, 0); return START_STICKY; }现在我在DataProcessor类接收数据处理器类的接收方法时播放铃声:
@Override public void onReceive(Context ctx,Intent intent) { playRIng(ctx); }但是,当我运行此代码时,单击按钮,服务已创建但警报在调用AlarmService后立即触发,并且还播放铃声。如果可能,因为我在注册广播时给出了准确的7 O时钟时间。 谷歌搜索了很多,但只发现相同的代码,没有别的。每个代码都可以播放当时的铃声,但它也会在广播注册后立即播放铃声。
I am trying to play a ringtone at exactly at 7 PM everyday but it is playing ringtone immediately after its pending intent is registering broadcast.
I called the service in the foreground on a button click and created pending intent there in onStartCommand:
@Override public int onStartCommand(Intent intent, int flags, int startId) { startForeground(FOREGROUND_ID, buildForegroundNotification("DummyApp")); c = Calendar.getInstance(); AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); int interval = 1000 * 60 * 60*24; c.setTimeInMillis(System.currentTimeMillis()); c.set(Calendar.HOUR, 19); c.set(Calendar.MINUTE,00); manager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), interval, pendingIntent); Intent alarmIntent = new Intent(AlarmService.this, DataProcessor.class); pendingIntent = PendingIntent.getBroadcast(AlarmService.this, 0, alarmIntent, 0); return START_STICKY; }Now I am playing a ringtone on receiving this broadcast in DataProcessor class the on Receive method of Data Processor class:
@Override public void onReceive(Context ctx,Intent intent) { playRIng(ctx); }But when I run this code,click the button,service is created but alarm is fired immediately after the AlarmService is called and ringtone is played also.How it is possible because I am giving the exact 7 O clock time when registering broadcast.? Googled a lot but found the same code only and nothing else.Every code is able to play the ringtone on the time but it also plays the ringtone immediately after the broadcast is registered.
最满意答案
您可以尝试在开发人员网站上的示例,了解那里可用的示例代码。可能在您的代码中存在一些编程错误,但可用的示例代码正是您想要的。
调度重复警报
You can try the example at developers website understanding the samplecode available there.May be there is some programming mistake in your code but the sample code available there is working exactly you want.
Scheduling Repeating Alarms
更多推荐
发布评论