Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
Set Notification Automatically

I created an application that sends Notifications every 2 hours using a BackgroundThread extends Thread class, it works very well,but ones I close the application ,I don't recieve any notification, could you pease help me.

Here is my code.

the class BackgroundThread extends Thread

public class BackgroundThread extends Thread {

    boolean running = true;
    final static String ACTION = "NotifyServiceAction";
    NotificationManager notificationManager;
    Notification myNotification;
    Context context;
    static int idnotification=100;

    public BackgroundThread(Context context)
    {
        this.context=context;
    }

    public void setRunning(boolean running) {
        this.running = running;
    }


    public synchronized void start() {
        super.start();
        notificationManager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);}


    public void run() {
        while(running){//
            try{
                sleep(10000);//l'envoi de lapplication chaque 2 h
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

             myNotification = new Notification(R.drawable.aramtres100,"Notification",System.currentTimeMillis());
            context = context.getApplicationContext();
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.valueOf(myNotification)));
            //Intent i = new Intent(this.context,NotifRecep.class);
            PendingIntent pendingIntent= PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder builder= (NotificationCompat.Builder) new NotificationCompat.Builder(context).setContentIntent(pendingIntent)
                    .setSmallIcon(android.R.drawable.arrow_up_float)
                    .setContentTitle("Alarme Medicament")
                    .setContentText("Il est temps de prendre ton medicament")
                    .setAutoCancel(true)
                    .setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.newmessage));
            notificationManager.notify(idnotification,builder.build());
            idnotification++;
        }

    }
}

And here is the MainActivity:

public class MainActivity extends Activity {
    BackgroundThread backgroundThread;


    protected void onStart() {
        super.onStart();
        backgroundThread = new BackgroundThread(this);
        backgroundThread.setRunning(true);
        backgroundThread.start();
    }


    protected void onStop() {
        super.onStop();
        boolean retry = true;
        backgroundThread.setRunning(false);

        while (retry) {
            try {
                backgroundThread.join();
                retry = false;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda