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