vendredi 11 mars 2016

Android alarm manager not updating sqlite data permanently

I have a task which notifies the phone at a specific time only when the specific column = 0. After the alarm goes on, an alertdialog pops up which when the ok is clicked, the column value will change to 1. That stops the alarm from going but when I restart the app, the column value turns back to 0.

Alarm Receiver

public class AlarmReceiver extends BroadcastReceiver {

public static final String TAG = AlarmReceiver.class.getSimpleName();
public static final String ACTION_ALARM_RECEIVER = "ACTION_ALARM_RECEIVER";
MediaPlayer mp;

@Override
public void onReceive(final Context context, Intent intent) {
    String message = "Hellooo, alrm worked ----";
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    String name = intent.getStringExtra("name");
    final String id = intent.getStringExtra("id");
    final String notify = intent.getStringExtra("notify");
    mp = MediaPlayer.create(context, R.raw.babycry);

    mp.start();

    final DBHandler db = new DBHandler(context);
    db.alarmDone(Integer.parseInt(notify));
    new AlertDialog.Builder(HomeActivity.getInstace())
            .setTitle(name)
            .setMessage("The baby needs to " + name + " " + id + " " + notify)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // continue with delete
                    db.alarmDone(Integer.parseInt(notify));
                   //NotificationTableModel notification = new NotificationTableModel();
                    //db.deleteAll(notification);
                    Toast.makeText(context, id, Toast.LENGTH_SHORT).show();
                    mp.stop();
                }
            })
            .setIcon(android.R.drawable.btn_star)
            .show();

    //db.setNotified();
    try {
        //HomeActivity  .getInstace().updateTheTextView(message);
       /* if(MainActivity.getInstace().alarmSet == true) {
            MainActivity.getInstace().setAlarm(2,5);
            MainActivity.getInstace().alarmSet = false;
        }*/
    } catch (Exception e) {

    }
    /*Intent intent2 = new Intent(context, MainActivity.class);
    intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent2);*/


    }

}

function that changes column value from 0 to 1

public void alarmDone(int taskid) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("UPDATE "+ TABLE_NOTIFICATION +" SET read=1 WHERE 1");
    /*ContentValues newValues = new ContentValues();
    newValues.put(KEY_READ, 2);

    String[] args = new String[]{String.valueOf(taskid)};
    db.update(TABLE_NOTIFICATION, newValues, "taskid=?", args);*/
}

Aucun commentaire:

Enregistrer un commentaire