-1

I want to have an action (the one in Runnable) that runs in the background even after the app gets killed. I have errors at r inThread t = new Thread(r); (in Thread cannot be applied java Runnable) and at t.start();(cannot resolve method)

This is in onCreate()

Runnable r = new Runnable() {
    @Override
    public void run() {

        Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
        SharedPreferences sharedPref = getSharedPreferences("Alarms", Context.MODE_PRIVATE);
        mh = sharedPref.getInt("morningHour",  hour_x );
        mm = sharedPref.getInt("morningMinute",  minute_x );
        int hours = cal.get(Calendar.HOUR_OF_DAY);
        int minutes = cal.get(Calendar.MINUTE) ;

        if(hours == mh && minutes == mm)
            startAlarm();

        nh = sharedPref.getInt("noonHour",  hour_x );
        nm = sharedPref.getInt("noonMinute",  minute_x );

        if(hours == nh && minutes == nm)
            startAlarm();

        eh = sharedPref.getInt("eveningHour",  hour_x );
        em = sharedPref.getInt("eveningMinute",  minute_x );

        if(hours == eh && minutes == em)
            startAlarm();
    }
};

Thread t = new Thread(r);
t.start();
krlzlx
  • 5,752
  • 14
  • 47
  • 55

2 Answers2

0

Thread is killed after it's parent process is killed, if you want your thread to keep processing you can start it in a separate process, refer to this for more information.

If you need results from your thread try passing information between the two processes, this can be achieved using multiple ways. refer to this for more information.

Akhadra
  • 419
  • 3
  • 10
0

The key is that you could create a thread that is a daemon or not. The daemon thread it is a thread that terminates after the main thread terminates. In your case you need to create a thread that is not a daemon thread. You could simply call the method

t.setDaemon(false)

before starting your thread.

The whole code snippet is:

package com.stackoverflow;

public class Main {

public static void main(String[] args) {
    Main main = new Main();
    main.onCreate();
}

public void onCreate() {
    Runnable r = new Runnable() {
        @Override
        public void run() {

            Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
            SharedPreferences sharedPref = getSharedPreferences("Alarms", Context.MODE_PRIVATE);
            mh = sharedPref.getInt("morningHour",  hour_x );
            mm = sharedPref.getInt("morningMinute",  minute_x );
            int hours = cal.get(Calendar.HOUR_OF_DAY);
            int minutes = cal.get(Calendar.MINUTE) ;
            if(hours == mh && minutes == mm)
                startAlarm();

            nh = sharedPref.getInt("noonHour",  hour_x );
            nm = sharedPref.getInt("noonMinute",  minute_x );
            if(hours == nh && minutes == nm)
                startAlarm();

            eh = sharedPref.getInt("eveningHour",  hour_x );
            em = sharedPref.getInt("eveningMinute",  minute_x );
            if(hours == eh && minutes == em)
                startAlarm();
        }
    };

    Thread t = new Thread(r);
    t.setDaemon(false);
    t.start();
    }
}
Sergey Chepurnov
  • 1,397
  • 14
  • 23
  • If he wants to have a background process running, surely he actually wants to have a daemon thread running when the main thread is killed? – Cowboy Farnz Apr 25 '18 at 15:59