1

I created a simple android app that will send notification every minutes. For that I use Service in this app. Look the Service code bellow.

public class notiService extends Service {
    private final static int interval = 1000 * 60;
    Handler myHandler;
    Runnable myRunable;
    MediaPlayer mp;
    Intent intent;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) { 
        mp = MediaPlayer.create(this,R.raw.noti2);
        createRunnable();
        startHandler();
        return Service.START_STICKY;
    }

    @Override
    public void onDestroy() {
        /**
         * Destroy Handler and Runnable
         */
        myHandler.removeCallbacks(myRunable);
        super.onDestroy ();
    }

    /**
     * Runnable method
     */
    public void createRunnable(){
        myRunable = new Runnable() {
            @Override
            public void run() {
                mp.start();
                send_notification("Notification title", "10");
                myHandler.postDelayed(this, interval); /* The interval time */
            }
        };
    }

    /**
     * Handler method
     */
    public void startHandler(){
        myHandler = new Handler();
        myHandler.postDelayed(myRunable, 0);
    }

    /**
     * Notification method
     */
    public void send_notification(String title, String min){
        intent = new Intent(getApplicationContext(),MainActivity.class);
        //intent.putExtra("open_fragment","open_f2");
        PendingIntent my_pIntent = PendingIntent.getActivities(notiService.this,0, new Intent[]{intent},0);
        Notification mynoti = new Notification.Builder(notiService.this)
                .setContentTitle(title)
                .setContentText("It will be start after "+min+" minutes.")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(my_pIntent).getNotification();
        mynoti.flags = Notification.FLAG_AUTO_CANCEL;
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(0,mynoti);
    }
}

It work properly when the app running. But if I close the app and device go to sleep mode, this code don't work properly. This time it send notification after 10 minutes or more.

I can't understand why it behave like this! How I can fixed this problem? Thank you for your response.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
shariful islam
  • 389
  • 4
  • 18
  • Service may not work properly as the android device may kill it or when it goes into doze mode such services can b suspended you may consider using JobSchedular to avoid this problem Refer this https://stackoverflow.com/a/49533086/4762767 – Akshay Katariya Apr 25 '18 at 09:05

1 Answers1

0

you are using handler for this. Handler does not work when device goes to sleep . you can see this link for running handler in sleep mode

Hitesh Sarsava
  • 666
  • 4
  • 15
  • I can't understand what he want to say `handler.postDelayed(new Runnable(),1000);` in this line! What is the relation **mHandler** with **handler**? – shariful islam Apr 26 '18 at 09:07