1

I try to run this code to get the status of the user from my database. The process includes JSON, so I make a new class implements Runnable to act as background service. I did create a class extends Service. In that Service I call the thread. I use handler and use postDelayed to repeat the thread.

public int onStartCommand(Intent intent, int flags, final int startId) {

Toast.makeText(this,"SESSION START",Toast.LENGTH_SHORT).show();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        new Thread(new BackgroundThread(startId)).start();
    }
},1000);

return START_STICKY;
}

I believe that my codes here are for repeating the BackgroundThread.class implements Runnable

public class BackgroundThread implements Runnable {
int service_id;
int mark=-1;

public BackgroundThread(int service_id) {
    this.service_id=service_id;
}

@Override
public void run() {
    JSONData jsonData = new JSONData();
    if (jsonData.getJSONstring() == 1 && mark != 1) {
        Log.e("STATUS", "" + jsonData.getJSONstring());
        mark = 1;
    } else if (jsonData.getJSONstring() == 0 && mark != 0) {
        Log.e("STATUS", "" + jsonData.getJSONstring());
        mark = 0;
    }
    Log.d("RUNNING","RUNNING");
}
}

But the thread only happens once

11-09 23:38:56.683    6483-6526/com.example.asus.intentservice I/OpenGLRenderer﹕ Initialized EGL, version 1.4
11-09 23:38:58.743    6483-6526/com.example.asus.intentservice V/RenderScript﹕ 0xa34ed000 Launching thread(s), CPUs 4
11-09 23:38:59.642    6483-6745/com.example.asus.intentservice W/System﹕ ClassLoader referenced unknown path: /system/framework/tcmclient.jar
11-09 23:38:59.968    6483-6745/com.example.asus.intentservice E/STATUS﹕ 0
11-09 23:38:59.968    6483-6745/com.example.asus.intentservice D/RUNNING﹕ RUNNING

Is there something wrong with my codes? Or maybe there are alternatives to achieve my purpose. But I prefer if someone could help me to fix my codes. Very much appreciate it

  • *Is there something wrong with my codes?*, yes - postDelayed posts only once. What are you trying to achieve ? – Blackbelt Nov 09 '16 at 16:57
  • I need the thread to run repetitively checking the status of the user. I have been searching the internet and most webs suggest to use timer or handler to achieve that. Am I wrong in understanding it? I am new to android studio. Thanks @Blackbelt – Dhani Himawan Nov 09 '16 at 17:08
  • postDelayed posts a single runnable once. If you want to post it multiple times, you have the runnable postDelayed itself again. Although the combination of doing this with a thread is a smell to say the least- you probably shouldn't have the runnable and should just have an infinitely looping thread. – Gabe Sechan Nov 09 '16 at 17:11
  • @Gabe So if I postDelayed it again, it just run twice? Do you have any suggestions for me to make the runnable constantly checks my database? – Dhani Himawan Nov 09 '16 at 17:15
  • I wouldn't use runnables for that at all. If you want to do a network request every X seconds, use a Thread. Using multiple threads started by reposting a handler is just asking for a lot of problems. For example what if the request at time T=0 finishes after the request at time T=1? – Gabe Sechan Nov 09 '16 at 17:16
  • Am I not using thread in my codes? new Thread(new BackgroundThread(startId)).start(); @Gabe – Dhani Himawan Nov 09 '16 at 17:21
  • You're creating a new thread every request. You only need one looping thread total – Gabe Sechan Nov 09 '16 at 17:23
  • I am sorry I don't follow. Could you give me an example? @Gabe – Dhani Himawan Nov 09 '16 at 17:26

1 Answers1

0

After get better understanding about handlers postDelayed that was explained by others, I try to look for an alternative and try using this code from Android run thread in service every X seconds and it works. Hope my question helps others who lost like me This is my new code

public int onStartCommand(Intent intent, int flags, final int startId) {

Toast.makeText(this,"SESSION START",Toast.LENGTH_SHORT).show();
    ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5);
    scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            new Thread(new BackgroundThread(startId)).start();
        }
    },0,2, TimeUnit.SECONDS);
return START_STICKY;
}
Community
  • 1
  • 1