Initializing a Handler inside JobIntentService when offline invokes this issue
Caused by java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at JobIntentSericeClass.calculateExtras(DataManager.java:288)
at JobIntentSericeClass.onHandleBlockingIntent(DataManager.java:499)
at JobIntentSericeClass.onHandleWork(DataManager.java:100)
at android.support.v4.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:391)
at android.support.v4.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:382)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Please refer the following code:
public class JobIntentServiceClass extends JobIntentService {
@Override
protected void onHandleWork(@NonNull Intent intent) {
onHandleBlockingIntent(intent);
}
private void onHandleBlockingIntent(Intent intent) {
calculateExtras();
}
private void calculateExtras() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// some async task here
}
}
}, 5000);
}
}
I got an error on the line where handler is initialized. And also it happens when the app is offline. Online scenario works fine. Seems really strange