Hello. I am new to Android programming. For college, you need to make some changes to the project. In my project, when the alarm goes off, to turn it off, you need to hold your finger down. It is necessary to change the conditions so that the alarm is turned off after solving a simple mathematical example (for example, 31 + 16 = enter the answer). On layoute, which is called when the alarm goes off, I created EditText and Button for input, but I don’t know what code to write. I tried a lot of options, or no reaction, or a lot of mistakes. Here is what is contained in the java file:

package com.home.abel.wakemeup; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.widget.Toast; import java.util.Calendar; /** * Created by abel on 3/2/16. */ public class AlarmManagement { public static final int WITH_DATABASE = 1; public static void enableAlarm(Context context, Alarm alarm, int flag) { Intent intent = new Intent(context, MyReceiver.class); Bundle bundle = new Bundle(); bundle.putInt("hh", alarm.getHh()); bundle.putInt("mm", alarm.getMm()); bundle.putInt("id", alarm.getId()); bundle.putString("rpt", alarm.getRepeat()); bundle.putString("text", alarm.getText()); bundle.putString("state", alarm.getState()); intent.putExtras(bundle); AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); PendingIntent pendingIntent = PendingIntent.getBroadcast( context, alarm.getHh() * 100 + alarm.getMm(), intent, 0 ); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, alarm.getHh()); calendar.set(Calendar.MINUTE, alarm.getMm()); if(System.currentTimeMillis() >= calendar.getTimeInMillis()) { calendar.add(Calendar.DAY_OF_WEEK, 1); } if(alarm.getRepeat().equals(context.getString(R.string.text_onetime))) { manager.set(AlarmManager.RTC_WAKEUP, ///ONETIME calendar.getTimeInMillis(), pendingIntent ); } else if(alarm.getRepeat().equals(context.getString(R.string.text_daily))) { manager.setRepeating(AlarmManager.RTC_WAKEUP, ///DAILY calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent ); } else if(alarm.getRepeat().equals(context.getString(R.string.text_weekly))) { manager.setRepeating(AlarmManager.RTC_WAKEUP, ///WEEKLY calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent ); } if(flag == WITH_DATABASE) { AlarmDBHelper dbHelper = new AlarmDBHelper(context); SQLiteDatabase database = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(AlarmContract.AlarmsEntry.COLUMN_ENABLED, AlarmContract.ENABLED); String selection = AlarmContract.AlarmsEntry._ID + " LIKE ?"; String[] selectionArgs = { String.valueOf(alarm.getId()) }; database.update( AlarmContract.AlarmsEntry.TABLE_NAME, values, selection, selectionArgs ); database.close(); dbHelper.close(); } } public static void updateAlarm(Context context, Alarm alarm) { Intent intent = new Intent(context, MyReceiver.class); Bundle bundle = new Bundle(); bundle.putInt("hh", alarm.getHh()); bundle.putInt("mm", alarm.getMm()); bundle.putInt("id", alarm.getId()); bundle.putString("rpt", alarm.getRepeat()); bundle.putString("state", alarm.getState()); bundle.putString("text", alarm.getText()); intent.putExtras(bundle); AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); PendingIntent pendingIntent; int solution = 45-12; int userSolution = 36; if(solution!=userSolution) { pendingIntent = PendingIntent.getBroadcast( context, alarm.getHh() * 100 + alarm.getMm(), intent, PendingIntent.FLAG_UPDATE_CURRENT ); manager.cancel(pendingIntent); } else pendingIntent = PendingIntent.getBroadcast( context, alarm.getHh() * 100 + alarm.getMm(), intent, PendingIntent.FLAG_NO_CREATE ); /* PendingIntent pendingIntent = PendingIntent.getBroadcast( context, alarm.getHh() * 100 + alarm.getMm(), intent, PendingIntent.FLAG_UPDATE_CURRENT); */ Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, alarm.getHh()); calendar.set(Calendar.MINUTE, alarm.getMm()); calendar.add(Calendar.DAY_OF_WEEK, 1); if(alarm.getRepeat().equals(context.getString(R.string.text_onetime))) { manager.set(AlarmManager.RTC_WAKEUP, ///ONETIME calendar.getTimeInMillis(), pendingIntent ); } else if(alarm.getRepeat().equals(context.getString(R.string.text_daily))) { manager.setRepeating(AlarmManager.RTC_WAKEUP, ///DAILY calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent ); } else if(alarm.getRepeat().equals(context.getString(R.string.text_weekly))) { manager.setRepeating(AlarmManager.RTC_WAKEUP, ///WEEKLY calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent ); } } public static void cancelAlarm(Context context, Alarm alarm) { Intent intent = new Intent(context, MyReceiver.class); ///!!!!!!! AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); PendingIntent pendingIntent; int solution = 45-12; int userSolution = 36; if(solution!=userSolution) { pendingIntent = PendingIntent.getBroadcast( context, alarm.getHh() * 100 + alarm.getMm(), intent, PendingIntent.FLAG_CANCEL_CURRENT ); manager.cancel(pendingIntent); } else pendingIntent = PendingIntent.getBroadcast( context, alarm.getHh() * 100 + alarm.getMm(), intent, PendingIntent.FLAG_NO_CREATE ); AlarmDBHelper dbHelper = new AlarmDBHelper(context); SQLiteDatabase database = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(AlarmContract.AlarmsEntry.COLUMN_ENABLED, AlarmContract.DISABLED); String selection = AlarmContract.AlarmsEntry._ID + " LIKE ?"; String[] selectionArgs = { String.valueOf(alarm.getId()) }; database.update( AlarmContract.AlarmsEntry.TABLE_NAME, values, selection, selectionArgs ); database.close(); dbHelper.close(); } } 

Thanks in advance for any help.

  • What options have you tried? which mistakes? - Android Android
  • @ Android for the beginning I tried the condition int solution = 45-12; int userSolution = 36; if(solution==userSolution) manager.cancel(pendingIntent); else { return; } int solution = 45-12; int userSolution = 36; if(solution==userSolution) manager.cancel(pendingIntent); else { return; } int solution = 45-12; int userSolution = 36; if(solution==userSolution) manager.cancel(pendingIntent); else { return; } I set the wrong condition to check if there will be any reaction. But the alarm clock was safely turned off and provided. After that, I slightly changed this condition, but nothing helped. Then I tried to implement it with the help of AlertDialog, which should appear with the launch of the alarm clock and disappear after entering the correct answer, but I did not work with AlertDialog before. - Misha Kolosov

0