No one
No one

Reputation: 1140

Alarm is not awaking on time

I have created setAlarmAt(int hours,int minutes) function and I am calling this function on button click with argument of current hour and current minute taking by system. I am also broadcasting it using Intent to ReminderReceiver class that will ring a music and vibrate. But the alarm is not awaking at given time like if i am setting it setAlarmAt(10,20) then it should wake up on 10:20 AM... My function is here...

'

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

    }

`

and Manifest.xml code is as

    <?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>

and ReminderReceiver.class file is

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();  
    }

}

'

Upvotes: 1

Views: 85

Answers (2)

Jaykishan Sewak
Jaykishan Sewak

Reputation: 822

Hey Brijesh Vishwakarma you have problem with time that you are setting

please replace following line

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

I am sure it will work for you because its working for me.

Upvotes: 1

Andrey E
Andrey E

Reputation: 866

I suppose that your manifest.xml isn't correct. Try to add the full package name when you register the receiver.

It should looks something like this:

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

Upvotes: 0

Related Questions