0

I want to set a reminder at 12 p.m daily when the user on the switch. On isChecked() I have setText as "Remind at 12:00 PM". I want to set an alarm or reminder there. The code is as follows :

 private RadioButton radioButtonPlayback, radioButtonTranslate;
    private TextView switchStatus;
    private Switch mySwitch;

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


        switchStatus = (TextView) findViewById(R.id.switchStatus);
        mySwitch = (Switch) findViewById(R.id.mySwitch);

        //set the switch to ON
        mySwitch.setChecked(true);
        //attach a listener to check for changes in state
        mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {
                    switchStatus.setText("Remind at: 12:00 PM");

                } else {
                    switchStatus.setText("No reminders set");
                }

            }
        });

        //check the current state before we display the screen
        if (mySwitch.isChecked()) {
            switchStatus.setText("Remind at: 12:00 PM");
        } else {
            switchStatus.setText("No reminders set");
        }
Salman Shaikh
  • 127
  • 10

1 Answers1

0

Try this :

 mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()

    {

        @Override
        public void onCheckedChanged (CompoundButton buttonView,boolean isChecked){
        if (isChecked) {
            Intent myIntent = new Intent(MainActivity.this, AlarmReceiver AlarmReceiver.class);
            pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, 12);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 0);
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        }
    }
    }

    );

AlarmReceiverClass :

public class AlarmReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        //this will update the UI with message
        AlarmActivity inst = AlarmActivity.instance();
        inst.setAlarmText("Alarm! Wake up! Wake up!");

        //this will sound the alarm tone
        //this will sound the alarm once, if you wish to
        //raise alarm in loop continuously then use MediaPlayer and setLooping(true)
        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        if (alarmUri == null) {
            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        }
        Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
        ringtone.play();

        //this will send a notification message
        ComponentName comp = new ComponentName(context.getPackageName(),
                AlarmService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

AlarmService Class:

public class AlarmService extends IntentService {
    private NotificationManager alarmNotificationManager;

    public AlarmService() {
        super("AlarmService");
    }

    @Override
    public void onHandleIntent(Intent intent) {
        sendNotification("Wake Up! Wake Up!");
    }

    private void sendNotification(String msg) {
        Log.d("AlarmService", "Preparing to send notification...: " + msg);
        alarmNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, AlarmActivity.class), 0);

        NotificationCompat.Builder alamNotificationBuilder = new NotificationCompat.Builder(
                this).setContentTitle("Alarm").setSmallIcon(R.drawable.ic_launcher)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg);


        alamNotificationBuilder.setContentIntent(contentIntent);
        alarmNotificationManager.notify(1, alamNotificationBuilder.build());
        Log.d("AlarmService", "Notification sent.");
    }
}

In menifest :

 <service
            android:name=".AlarmService"
            android:enabled="true" />
        <receiver android:name=".AlarmReceiver" />
Nidhi
  • 777
  • 7
  • 17
  • I don't think so. Have you tried the code? Are you getting any permission error in log? – Nidhi Dec 27 '16 at 06:51
  • Yes I tried the code but I am not getting any alarm notification. What should I write in MyAlarmService.class file? – Salman Shaikh Dec 27 '16 at 06:57
  • Still not getting it. Where is AlarmService.class and MyAlarmService.class ? – Salman Shaikh Dec 27 '16 at 07:27
  • AlarmService.class is a custom class for which I have added the code and since AlarmReceiverClass is extending a WakefulBroadcastReceiver, you have to register it in manifest too. – Nidhi Dec 27 '16 at 07:31