gpt4 book ai didi

android - 闹钟未按时唤醒

转载 作者:行者123 更新时间:2023-11-30 02:15:48 27 4
gpt4 key购买 nike

我已经创建了 setAlarmAt(int hours,int minutes) 函数,我在单击按钮时调用此函数,参数是系统采用的当前小时和当前分钟。我还使用 Intent 广播它到 ReminderReceiver 类,它会响起音乐并振动。但是闹钟在给定时间没有醒来,就像我设置它 setAlarmAt(10,20) 那么它应该在上午 10:20 醒来......我的功能在这里...

'

public void setAlarmAt(int hours,int minutes)
{
Intent reminderintent=new Intent(MainActivity.this,ReminderReceiver.class);
final int alarmId = (int) System.currentTimeMillis();//to distinquish different-2 reminders
pendingIntent=PendingIntent.getBroadcast(MainActivity.this, alarmId, reminderintent, 0);//if id is same then it will replace existing and also we can't cancel it without proper id
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000 * 60 * 20;

/* Set the alarm to start at 10:30 AM */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(calendar.SECOND, pendingIntent.FLAG_UPDATE_CURRENT);

/* Repeating on every 20 minutes interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, pendingIntent);
showmessage("Reminder Scheduled at:"+hours+":"+minutes);

}

`

Manifest.xml代码如下

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.reminder"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="ReminderReceiver"></receiver>
</application>

</manifest>

ReminderReceiver.class文件是

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Vibrator;
import android.widget.Toast;

public class ReminderReceiver extends BroadcastReceiver{
MediaPlayer mp;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
mp=MediaPlayer.create(context, R.raw.alarmtone);
mp.start();
Vibrator v = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
}

}

'

最佳答案

嘿 Brijesh Vishwakarma 你设置的时间有问题

请替换下一行

manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, pendingIntent);

To

manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTime().getTime(),
1000 * 60 * 20, pendingIntent);

and remove following line

calendar.set(calendar.SECOND, pendingIntent.FLAG_UPDATE_CURRENT);

我相信它对你有用,因为它对我有用。

关于android - 闹钟未按时唤醒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29374140/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com