Ehsan
Ehsan

Reputation: 197

Alarm not going off when it should

I'm working on AlarmManager; I've created an alarmManager (based on a tutorial) which can be set to go off at a specific time, but the problem here is that as soon as I set time and click the ok button in the TimePickerDialog the alarm goes off no matter when it should. However, the toast is shown when it's supposed to.

What am I missing?

Here is my code:

AndroidTimeActivity.java

public class AndroidTimeActivity extends Activity {

NotificationCompat.Builder mBuilder;
TimePicker myTimePicker;
Button buttonstartSetDialog;
TextView textAlarmPrompt;

TimePickerDialog timePickerDialog;

final static int RQS_1 = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textAlarmPrompt = (TextView)findViewById(R.id.alarmprompt);

buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
buttonstartSetDialog.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
textAlarmPrompt.setText("");
openTimePickerDialog(false);

        }
    });

}


private void openTimePickerDialog(boolean is24r){
    Calendar calendar = Calendar.getInstance();

    timePickerDialog = new TimePickerDialog(
    AndroidTimeActivity.this,
    onTimeSetListener,
    calendar.get(Calendar.HOUR_OF_DAY),
    calendar.get(Calendar.MINUTE),
    is24r);
    timePickerDialog.setTitle("Set Alarm Time");

    timePickerDialog.show();

}

OnTimeSetListener onTimeSetListener
    = new OnTimeSetListener(){

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

    Calendar calNow = Calendar.getInstance();
    Calendar calSet = (Calendar) calNow.clone();

    calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calSet.set(Calendar.MINUTE, minute);
    calSet.set(Calendar.SECOND, 0);
    calSet.set(Calendar.MILLISECOND, 0);

    if(calSet.compareTo(calNow) <= 0){

    calSet.add(Calendar.DATE, 1);
        }

        setAlarm(calSet);
    }
};

private void setAlarm(Calendar targetCal){

    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

    mBuilder =
            new NotificationCompat.Builder(this);
                    mBuilder.setSmallIcon(R.drawable.icon1);
                    mBuilder.setContentTitle("My notification");
                    mBuilder.setContentText("Hello World!");
    mBuilder.setContentIntent(pendingIntent);

    mBuilder.setSound(Uri.parse("android.resource://com.example.applenett.myapplication/raw/ringtone1"));




    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);




    notificationManager.notify(0, mBuilder.build());


}

}

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {

    Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();

}

}

AndroidManifest.xml

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".AndroidTimeActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <receiver
        android:name=".AlarmReceiver"
        android:label="@string/title_activity_alarm_receiver"
        android:theme="@style/AppTheme.NoActionBar"
        android:process=":remote">
    </receiver>
</application>

Upvotes: 2

Views: 95

Answers (1)

Shmuel
Shmuel

Reputation: 3916

You are calling notificationManager.notify(0, mBuilder.build()); right then so that notification (and the sound accompanying it) will fire then b/c you've asked it to. You should move the whole notification builder (and notify) section to the alarm receiver class so it will get triggered along with the toast at that time.

Upvotes: 1

Related Questions