Android : AlarmManager is not working properly. It is not able to set the alarm

1.5k views Asked by At

Here is my code Code to set Alarm. This is the code I tried, but unfortunately it won't set the Alarm at all. Please help me with this. The AlarmManager is the only way I know to set alarm. Could anyone tell me if there is some other way. I have given all the necessary permissions in the manifest also.

  public class NextAlarm extends Activity {

            DatePicker pickerDate;
            TimePicker pickerTime;
            Button buttonSetAlarm;
            TextView info;

            final static int RQS_1 = 1;
Method to get the date picker and time picker

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_next_alarm);

                info = (TextView)findViewById(R.id.info);
                pickerDate = (DatePicker)findViewById(R.id.pickerdate);
                pickerTime = (TimePicker)findViewById(R.id.pickertime);

                Calendar now = Calendar.getInstance();

                pickerDate.init(
                        now.get(Calendar.YEAR),
                        now.get(Calendar.MONTH),
                        now.get(Calendar.DAY_OF_MONTH),
                        null);

                pickerTime.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
                pickerTime.setCurrentMinute(now.get(Calendar.MINUTE));

                buttonSetAlarm = (Button)findViewById(R.id.setalarm);
                buttonSetAlarm.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View arg0) {
                        Calendar current = Calendar.getInstance();

                        Calendar cal = Calendar.getInstance();
                        cal.set(pickerDate.getYear(),
                                pickerDate.getMonth(),
                                pickerDate.getDayOfMonth(),
                                pickerTime.getCurrentHour(),
                                pickerTime.getCurrentMinute(),
                                00);

                        if(cal.compareTo(current) <= 0){
                            //The set Date/Time already passed
                            Toast.makeText(getApplicationContext(),
                                    "Invalid Date/Time",
                                    Toast.LENGTH_LONG).show();
                        }else{
                            setAlarm(cal);
                        }

                    }});
            }

            private void setAlarm(Calendar targetCal){

                info.setText("\n\n***\n"
                        + "Alarm is set@ " + targetCal.getTime() + "\n"
                        + "***\n");

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

                finish();
            }

        }

        _________________________________________________________________

        Receiver Class

        public class AlarmReceiver extends BroadcastReceiver {

            @Override
            public void onReceive(Context arg0, Intent arg1) {
                 notificationStatus(arg0);
}

private void notificationStatus(Context context) {
    final NotificationManager mNotificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);

    final int icon = R.drawable.ic_launcher;
    final Notification notification = new Notification(icon, "test", System.currentTimeMillis());
    final Intent notificationIntent = new Intent(context.getApplicationContext(), NextAlarm.class);
    final PendingIntent pIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, notificationIntent, 0);

    notification.setLatestEventInfo(context, "ticker", "title", pIntent);
    mNotificationManager.notify(1, notification);
}

        }

Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ctsprojects.com.alarmapp" >

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

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

    <uses-permission android:name="android.permission.VIBRATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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>
        <activity
            android:name=".songList"
            android:label="@string/title_activity_song_list" >
        </activity>
        <activity
            android:name=".NextAlarm"
            android:label="@string/title_activity_next_alarm" >
        </activity>

        <receiver android:name=".AlarmReceiver" android:enabled="true" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"></action>
            </intent-filter>
        </receiver>
    </application>

</manifest>
2

There are 2 answers

3
Surya On BEST ANSWER

This is how I was able to set alarm from my activity to ALARM app of Android. With AlarmManager its not possible it seems. Thank you for your answers.

Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
        l = getIntent().getExtras().getString("song");
        i.putExtra(AlarmClock.EXTRA_HOUR, pickerTime.getCurrentHour());
        i.putExtra(AlarmClock.EXTRA_MINUTES, pickerTime.getCurrentMinute());
        i.putExtra(AlarmClock.EXTRA_RINGTONE, l);
5
Hiren Patel On

Code for Set Alarm by Alarm Manager:

  AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
  Intent i1 = new Intent(this, ReceiveAlarmActivity.class);
  i1.putExtra("Key", "Value");
  PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i1, PendingIntent.FLAG_ONE_SHOT);
  if (Build.VERSION.SDK_INT >= 19){
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), operation);
   }else{
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), operation);
   }

Done