警报没有按时唤醒

时间:2015-03-31 16:52:16

标签: android

我创建了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();  
    }

}

2 个答案:

答案 0 :(得分:1)

嘿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);

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

答案 1 :(得分:0)

我认为你的manifest.xml是不正确的。 注册接收器时尝试添加完整的包名称。

看起来应该是这样的:

...
<receiver android:name="com.example.appname.AlarmReciever" >
        <intent-filter>
            <action android:name="com.example.android.receivers.AlarmReceiver" />
        </intent-filter>
</receiver>
...