runnable

Does a runnable in a service run on the UI thread

风格不统一 提交于 2019-11-29 10:20:10
问题 In Android, when I create a runnable inside a service and run it, while I realize it runs in its own thread, is this thread somehow part of the UI thread? In other words, if the runnable carried out a long process, would it affect the UI? EDIT: private class SomeRunnable implements Runnable { @Override public void run() { try { } } } SomeRunnable runnable = new SomeRunnable(); (new Handler()).postDelayed(runnable, 1000); 回答1: Docs: A services runs in the same process as the application in

SurfaceView shows black screen - Android

会有一股神秘感。 提交于 2019-11-29 10:08:06
Basically I want to use SurfaceView for animation. Therefore the class implements Runnable. To experiment, I want to draw a circle. However, it shows only a black screen. I have been trying for days. Really appreciate if someone can help. MainActivity class public class MainActivity extends Activity { private Bitmap Liquid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature (Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); DrawStripFrame D1

How to stop this thread in android?

≯℡__Kan透↙ 提交于 2019-11-29 05:22:46
In my application i am using this thread to rotate the Second hand on the clock. But the Problem is while i close the activity, the thread is still remain run. I want to stop the thread so it can not effect the Bettry of the device. Below is my Activity code where i am rotating the Clock's Second Hand: public class ClockActivity extends Activity implements OnClickListener{ private Button chimeBtn; private ImageView img; private Thread myThread = null; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.clock_layout); Runnable

how can I pass a variable into a new Runnable declaration? [duplicate]

不羁的心 提交于 2019-11-29 02:28:32
问题 This question already has answers here : How can I pass a parameter to a Java Thread? (18 answers) Closed 2 years ago . I have the following : Runnable done = new Runnable() { public void run() { System.out.println("Hello"); } }; And then in my Android activity I'll call something like : runOnUIThread(done); Which I then call. However, I want that "Hello" not to be hardcoded, so I can pass it in. Otherwise I'll have to have one of these declarations for every String I want to print. (This is

How to replace HashMap Values while iterating over them in Java

泄露秘密 提交于 2019-11-28 23:35:41
问题 I am using a Runnable to automatically subtract 20 from a players cooldown every second, but I have no idea how to replace the value of a value as I iterate through it. How can I have it update the value of each key? public class CoolDownTimer implements Runnable { @Override public void run() { for (Long l : playerCooldowns.values()) { l = l - 20; playerCooldowns.put(Key???, l); } } } 回答1: Using Java 8: map.replaceAll((k, v) -> v - 20); Using Java 7 or older: You can iterate over the entries

new Runnable() but no new thread?

。_饼干妹妹 提交于 2019-11-28 22:44:02
问题 I'm trying to understand the code here , specifically the anonymous class private Runnable mUpdateTimeTask = new Runnable() { public void run() { final long start = mStartTime; long millis = SystemClock.uptimeMillis() - start; int seconds = (int) (millis / 1000); int minutes = seconds / 60; seconds = seconds % 60; if (seconds < 10) { mTimeLabel.setText("" + minutes + ":0" + seconds); } else { mTimeLabel.setText("" + minutes + ":" + seconds); } mHandler.postAtTime(this, start + (((minutes * 60

java Runnable run() method returning a value

。_饼干妹妹 提交于 2019-11-28 21:03:30
According to java doc, Runnable method void run() cannot return a value. I do wonder however if there is any workaround of this. Actually i have a method which calls: public class Endpoint{ public method_(){ RunnableClass runcls = new RunnableClass(); runcls.run() } } wheren method run() is: public class RunnableClass implements Runnable{ public jaxbResponse response; public void run() { int id; id =inputProxy.input(chain); response = outputProxy.input(); } } I want to have acces to response variable in method_() is this possible? Narendra Pathai Use Callable<V> instead of using Runnable

Is there a way to pass parameters to a Runnable? [duplicate]

非 Y 不嫁゛ 提交于 2019-11-28 20:53:19
问题 This question already has answers here : Runnable with a parameter? [duplicate] (6 answers) Closed 4 years ago . I have a thread that uses a handler to post a runnable instance. it works nicely but I'm curious as to how I would pass params in to be used in the Runnable instance? Maybe I'm just not understanding how this feature works. To pre-empt a "why do you need this" question, I have a threaded animation that has to call back out to the UI thread to tell it what to actually draw. 回答1:

Why does Thread implement Runnable?

淺唱寂寞╮ 提交于 2019-11-28 20:12:57
A Java Thread's run() method is called by the JVM, on that thread, when the thread starts. To give a thread something to do, you can make a subclass of Thread and override its run() method, or (preferred) you can supply a Runnable to the thread's constructor. That's fine. I was in the midst of making a subclass of Thread and overriding run, and I realized I couldn't make the method protected as I expected to because Thread.run() is public. Then I realized why: it has to be public because Thread implements Runnable. But why does it implement Runnable? It doesn't seem logical. A thread is

How to remove a runnable from a handler object added by postDelayed?

梦想的初衷 提交于 2019-11-28 17:22:17
I have an "open" animation and am using Handler.postDelayed(Runnable, delay) to trigger a "close" animation after a short delay. However, during the time between open and close, there is possibly another animation triggered by a click. My question is, how would I cancel the "close" animation in the handler? Just use the removeCallbacks(Runnable r) method. Daniel L. Cristian's answer is correct, but as opposed to what is stated in the answer's comments, you actually can remove callbacks for anonymous Runnables by calling removeCallbacksAndMessages(null); As stated here : Remove any pending