android-handler

Android sending messages between fragment and service

喜夏-厌秋 提交于 2019-12-01 06:37:32
问题 I have a fragment with a button. When clicked it tells a service to start polling sensors and then insert the sensor data into a database on a background thread. When the button is pushed again, the service will stop. When the Stop button is pushed, there may still be tasks in the executor queue that is inserting into the DB, so during this time I want to display a progress dialog, and dismiss it once the entire queue is clear. The fragment with the button looks like this: public class

postDelayed() in a Service

北城余情 提交于 2019-12-01 04:28:03
I'm trying to restart service from itself in a few time. My code looks like this (inside the onStartCommand(...) ) Looper.prepare(); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(BackgroundService.this, BackgroundService.class); startService(intent); } }, 3 * 60000); Service is running in the foreground while this code executes, but it doesn't seem to call onStartCommand(...) . Is there any other way to restart service from itself in a few time? UPD: I've found out that it actually restarts service, but not in a

postDelayed() in a Service

南楼画角 提交于 2019-12-01 01:57:28
问题 I'm trying to restart service from itself in a few time. My code looks like this (inside the onStartCommand(...) ) Looper.prepare(); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(BackgroundService.this, BackgroundService.class); startService(intent); } }, 3 * 60000); Service is running in the foreground while this code executes, but it doesn't seem to call onStartCommand(...) . Is there any other way to restart

How to cancel a handler before time in Android code?

喜欢而已 提交于 2019-11-30 17:25:29
I create 1 minute delayed timer to shutdown service if it's not completed. Looks like this: private Handler timeoutHandler = new Handler(); inside onCreate() timeoutHandler.postDelayed(new Runnable() { public void run() { Log.d(LOG_TAG, "timeoutHandler:run"); DBLog.InsertMessage(getApplicationContext(), "Unable to get fix in 1 minute"); finalizeService(); } }, 60 * 1000); If I get job accomplished before this 1 minute - I would like to get this delayed thing cancelled but not sure how. You can't really do it with an anonymous Runnable. How about saving the Runnable to a named variable?

How to cancel a handler before time in Android code?

只谈情不闲聊 提交于 2019-11-30 16:29:10
问题 I create 1 minute delayed timer to shutdown service if it's not completed. Looks like this: private Handler timeoutHandler = new Handler(); inside onCreate() timeoutHandler.postDelayed(new Runnable() { public void run() { Log.d(LOG_TAG, "timeoutHandler:run"); DBLog.InsertMessage(getApplicationContext(), "Unable to get fix in 1 minute"); finalizeService(); } }, 60 * 1000); If I get job accomplished before this 1 minute - I would like to get this delayed thing cancelled but not sure how. 回答1:

Android AsyncTask [Can't create handler inside thread that has not called Looper.prepare()]

筅森魡賤 提交于 2019-11-30 12:58:21
问题 I've created an image upload AsyncTask based on a function. And after uploading, I get this error on onPostExecute() . I read up some StackOverflow answers on Runnable yet I kept getting the error over and over again despite implementing a different solution. My code : class uploadFile extends AsyncTask<String, String, String> { private ProgressDialog pDialog; /** * -------------------------------------------------------------------- * --------------------------------- Before starting

How to stop Handler Runnable?

非 Y 不嫁゛ 提交于 2019-11-30 11:47:24
问题 I am using a handler in the following program and I want to stop it when i=5 but the handler doesn't stop and run continuously. b1.setOnClickListener(new OnClickListener() { public void onClick(View v) { handler = new Handler(); runnable = new Runnable() { public void run() { try { Toast.makeText(getApplicationContext(), "Handler is working", Toast.LENGTH_LONG).show(); System.out.print("Handler is working"); if(i==5){ //Thread.currentThread().interrupt(); handler.removeCallbacks(runnable);

Android AsyncTask [Can't create handler inside thread that has not called Looper.prepare()]

纵然是瞬间 提交于 2019-11-30 04:29:58
I've created an image upload AsyncTask based on a function. And after uploading, I get this error on onPostExecute() . I read up some StackOverflow answers on Runnable yet I kept getting the error over and over again despite implementing a different solution. My code : class uploadFile extends AsyncTask<String, String, String> { private ProgressDialog pDialog; /** * -------------------------------------------------------------------- * --------------------------------- Before starting background thread * Show Progress Dialog */ @Override protected void onPreExecute() { super.onPreExecute();

How to stop Handler Runnable?

时光总嘲笑我的痴心妄想 提交于 2019-11-30 01:23:49
I am using a handler in the following program and I want to stop it when i=5 but the handler doesn't stop and run continuously. b1.setOnClickListener(new OnClickListener() { public void onClick(View v) { handler = new Handler(); runnable = new Runnable() { public void run() { try { Toast.makeText(getApplicationContext(), "Handler is working", Toast.LENGTH_LONG).show(); System.out.print("Handler is working"); if(i==5){ //Thread.currentThread().interrupt(); handler.removeCallbacks(runnable); System.out.print("ok"); Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_LONG).show(); } i++; }

What is the difference between View.postDelayed() and Handler.postDelayed() on the main thread?

末鹿安然 提交于 2019-11-29 17:26:18
问题 According to the documentation of Handler.postDelayed(Runnable r, long delayMillis) : Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached. On the other hand View.postDelayed(Runnable action, long delayMillis) : Causes the Runnable to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the user interface