Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda