0

I am developing an application where the user can create notifications that are triggered every x times, and where the time is provided by the user.

The notification registers well, but does not fire. Where would the problem be? Thank you in advance.

Mantras.java:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Mantras extends AppCompatActivity implements View.OnClickListener {

    EditText editHeures, editMinutes, editSecondes, editMantras;
    Button valider;
    long miliHeures, miliMinutes, miliSecondes, addition;
    GestionNotif gestionNotif;

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

        editMantras = (EditText) findViewById(R.id.editText_mantras);
        editHeures = (EditText) findViewById(R.id.editText_heures);
        editMinutes = (EditText) findViewById(R.id.editText_minutes);
        editSecondes = (EditText) findViewById(R.id.editText_secondes);

        valider = (Button) findViewById(R.id.btn_valider);
        valider.setOnClickListener(this);

        gestionNotif = new GestionNotif(this);
    }

    @Override
    public void onClick(View v) {

        //Vérifier que le champ nom soit rempli.
        if (editMantras.getText().toString().isEmpty())

        {
            Toast.makeText(this, "Merci de remplir le nom", Toast.LENGTH_SHORT).show();
        }
        //Vérifier que ce soit bien des chiffres et nombres qui soient rentrés.
        else if (!verifChiffre(editHeures.getText().toString()) && !verifChiffre(editMinutes.getText().toString()) && !verifChiffre(editSecondes.getText().toString())) //Vérifier si les champs n'ont rien d'autres que des chiffres.

        {
            Toast.makeText(this, "Merci d'entrer des chiffres", Toast.LENGTH_SHORT).show();
        }

        //Récupérer le texte. Récupérer les temps, les convertir en milisecondes et enregistrer la notification.
        else {

            conversionTemps();
            gestionNotif.ajouterNotif(addition, editMantras.getText().toString());
        }

        //Enregistrer dans le téléphone et créer la notification.
    }

    public boolean verifChiffre(String str) {
        try {
            double d = Double.parseDouble(str);
        } catch (NumberFormatException nfe) {
            return false;
        }
        return true;
    }

    public long conversionTemps() {

        if (editHeures.getText().toString().isEmpty()) {
            miliHeures = 0;
        } else {
            miliHeures = Long.valueOf(editHeures.getText().toString()) * 3_600_000;
        }

        if (editMinutes.getText().toString().isEmpty()) {
            miliMinutes = 0;
        } else {
            miliMinutes = Long.valueOf(editMinutes.getText().toString()) * 60_000;
        }

        if (editSecondes.getText().toString().isEmpty()) {
            miliSecondes = 0;
        } else {
            miliSecondes = Long.valueOf(editSecondes.getText().toString()) * 1000;
        }

        addition = miliHeures + miliMinutes + miliSecondes;
        return addition;
    }
}

GestionNotif.java:

import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.widget.Toast;

public class GestionNotif {

    private Context context;
    private String mantras;
    private long addition;

    SharedPreferences pref;

    // Constructeur
    public GestionNotif(Context context) {

        this.context = context;

        pref = PreferenceManager.getDefaultSharedPreferences(context);

        //pref.edit().putLong("monTemps", 100000); Exemple pour ajouter/modifier une valeur dans les Preferences

        //pref.getLong("monTemps", 0); Exemple pour récupérer une valeur dans les Preferences
    }

    public void ajouterNotif(long addition, String mantras) {

        if (addition > 0) {

            this.mantras = mantras;
            this.addition = addition;

            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(mantras)
                    .setContentText(mantras)
                    .setContentIntent(pendingIntent)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    //.setStyle(new NotificationCompat.BigTextStyle().bigText("Much longer text that cannot fit one line..."))
                    //.setTimeoutAfter(60000)
                    .setAutoCancel(true);

            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

            pref.edit().putLong("temps", addition).apply(); //Clé, valeur à enregistrer
            pref.edit().putString("mantras", mantras).apply(); //Clé, valeur à enregistrer

            // notificationId is a unique int for each notification that you must define
            notificationManager.notify(0, mBuilder.build()); // LANCE LA NOTIFICATION
            //TODO À lancer selon le temps indiqué par l'utilisateur

            temporisation();

            Toast.makeText(context, "Notification enregistrée: " + addition, Toast.LENGTH_SHORT).show();
        }
    }

    private void temporisation() {
        Intent notifyIntent = new Intent(context, MyReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast
                (context, 0, notifyIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC,  System.currentTimeMillis(),
                addition, pendingIntent);
    }

    // GETTERS
    public String getMantras() {
        return this.mantras;
    }

    public long getAddition() {
        return this.addition;
    }

    // SETTERS
    public void setmantras(String mantras) {
        this.mantras = mantras;
    }

    public void setAddition(long addition) {
        this.addition = addition;
    }
}
Martin Zeitler
  • 49,224
  • 12
  • 97
  • 156

0 Answers0