Adding and clearing window flags inside a thread - android

I want to add and clear window flag inside a thread but not working. Basically i want my thread to keep the screen on for two seconds and then clear the screen on flag.
Here's my code:
public class WriteCommThread extends Thread {
private long time=2000;
public WriteCommThread(float count) {
time = (long) count;
}
public void run() {
while(connectionUnAbort==true){
// Lock screen
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
slleep();
//Unlock screen
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
connectionUnAbort=false;
}
}
public void slleep(){
try {
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

I solved it after doing research, i am posting the referred link.
Answer Link
Here's my solution to my problem and it worked perfectly. I added these runnable classes in my background thread (run method). And after sleeping i could clear the flag to keep the screen on.
runOnUiThread(new Runnable() {
public void run() {
//stuff that updates ui
MainActivity.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});
runOnUiThread(new Runnable() {
public void run() {
//stuff that updates ui
MainActivity.this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});

Related

Android- Updating UI elements with threading

I have a textview which i want to change with a thread and do it again and again (Like a digital clock). But i'm having problems with setting time between 2 changes. Here, the code:
display1 = (TextView) findViewById(R.id.textView1);
Thread timer2 = new Thread(){
#Override
public void run() {
synchronized (this){
runOnUiThread(new Runnable() {
#Override
public void run() {
int i = 0;
display1.setText("" + i);
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
display1.setText("" + (i+1));
}
});
}
}
};
timer2.start();
This sleep(2000); function makes textview invisible for the given time but i want it stand still till the next change. How can i do that?
But i'm having problems with setting time between 2 changes
Do NOT do sleep() on your UI thread. If you want to chain some actions with the delay, split your code into two runnables and the first one should set display1 and then post second runnable with the delay using postDelayed()
EDIT
want one of them to increase 3 per sec, and the other 5 per sec until they reach 1000 for instance
You can make your Runnable post itself until some criteria are met (i.e. time, counter value etc). Just at the end your Runnable check your conditions and if not met, call postDelayed(this, delay); and you are good.
You should use a Handler instead and dispatch your changes to the UI thread.
Handler mHandler = new Handler();
mHandler.post(new Runnable() {
#Override
public void run() {
// Code to be run right away
}
});
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// Code to be run after 2 seconds
}
}, 2000);
Maybe split up what you need to do into separate methods. Here you have a UI method and a sleep method split up. Hope it helps
private void myMethod() {
new Thread() {
#Override
public void run() {
int i = 0;
doWorkOnUI(String.valueOf(i));
pause(2000);
doWorkOnUI(String.valueOf(i++));
}
}.start();
}
private void pause(int pauseTime) {
try {
Thread.sleep(pauseTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void doWorkOnUI(final String string) {
runOnUiThread(new Runnable() {
#Override
public void run() {
display1.setText(string);
}
});
}

Show fragment and close it after a few seconds without user interactions

I want to show a fragment for 5 seconds and then close it without the user interaction. This is my code.
#Override
public void onResume() {
super.onResume();
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(5000);
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
listener.closeFragment(null);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).run();
}
the listener.closeFragment(null);method works well (it close the fragment) but the fragment is never shown. Its like the thread runs before the fragment is visible. Any idea? is it posible?
Shouldn't you be using .start() instead of .run()?
You are just calling that that thread's logic sequentially.
Hope it helps.

Button does not disappear again when the layout is touched

final Thread timer = new Thread(){
public void run (){
try{
//sleep
sleep (2000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
nav2.setAnimation(alpha2);
nav2.setVisibility(View.INVISIBLE);
}
}
};
timer.start();
nav1.setOnClickListener(new LinearLayout.OnClickListener(){
#Override
public void onClick(View arg0) {
nav2.setVisibility(View.VISIBLE);
nav2.setAnimation(inleft);
timer.start();
}});
I have a code that will make button or linearlayout disappear after 2 secs in the timer the problem is that it crashes once the finally executes. Also the other button (nav1) is the whole linear layout so once it had been touched the nav2 or the buttons will appear and the timer will again reset
Generally, you can't touch your UI widgets from a background thread.
For your use case, consider a Handler on the UI thread and a Runnable posted with postDelayed().
try this instead of thread,
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
nav2.setAnimation(alpha2);
nav2.setVisibility(View.INVISIBLE);
} catch (Throwable e) {
e.printStackTrace();
}
}
}, 2000);

Thread in Android ---> Unfortunately ThreadApp has Stopped

I have one textview, which i can use to show countdown timer. I wanted to use Thread class which takes Runnable interface.
I wrote the following code for the same but it is gives run time error Unfortunately ThreadApp has Stopped
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity implements Runnable
{
TextView tvTimer;
Thread timerThread;
int time=30;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTimer =(TextView)findViewById(R.id.textView1);
timerThread= new Thread(this);
timerThread.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void run()
{
try
{
Thread.sleep(1000);
tvTimer.setText((String.valueOf(time)).toString());
time--;
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
At the same time when i removed tvTimer.setText((String.valueOf(time)).toString()); it works fine. Can anyone provide me the solution. I am new in android.
You can use updating UI using RunOnUiThread like this
#Override
public void run()
{
try
{
Thread.sleep(1000);
runOnUiThread(new Runnable() {
public void run() {
tvTimer.setText((String.valueOf(time)).toString());
}
});
time--;
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
when i removed tvTimer.setText((String.valueOf(time)).toString()); it
works fine
because you are trying to change TextView text from non-ui Thread so use runOnUiThread or Handler for updating TextView from other Thread
new Thread(new Runnable() {
#Override
public void run() {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
public void run() {
tvTimer.setText((String.valueOf(time)).toString());
time--;
}
});
}
You are trying to update ui in timer task. Timer task runs on a different thread. update ui on the ui thread. Use a handler or runonuithread
Handler m_handler;
Runnable m_handlerTask ;
m_handler = new Handler();
m_handlerTask = new Runnable()
{
#Override
public void run() {
tvTimer.setText((String.valueOf(time)).toString());
timer--;
// do something
m_handler.postDelayed(m_handlerTask, 1000); // instad of 1000 mention the delay in milliseconds
}
};
m_handlerTask.run();
Or use
#Override
public void run()
{
try
{
Thread.sleep(1000);
// should not call thread.sleep() bad design
// check the edit below
runOnUiThread(new Runnable(){
#Override
public void run(){
//update ui here
tvTimer.setText((String.valueOf(time)).toString());
}
});
time--;
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Edit: Using Thread.sleep() inside a thread is a bad design.
http://developer.android.com/training/articles/perf-anr.html
Quoting from the docs.
If you implement Thread or HandlerThread, be sure that your UI thread does not block while waiting for the worker thread to complete—do not call Thread.wait() or Thread.sleep(). Instead of blocking while waiting for a worker thread to complete, your main thread should provide a Handler for the other threads to post back to upon completion. Designing your application in this way will allow your app's UI thread to remain responsive to input and thus avoid ANR dialogs caused by the 5 second input event timeout.
To stops use this m_handler.removeCallbacks(m_handlerTask);
You are trying to update the UI element i.e., a TextView in your thread. You can't do this in a thread. Instead use a handler to post changes to your UI element as follows -
Handler handler = new Handler();
#Override
public void run()
{
try
{
Thread.sleep(1000);
handler.post(new Runnable()
{
public void run()
{
tvTimer.setText((String.valueOf(time)).toString());
}
time--;
});
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Updating UI using a Handler freezes my app

I am trying to make a clock, using a TextView :)
Someone here told me that I couldn't use normal threads to change the UI, but Handler or AsyncTask. I managed to get it working a few days ago, but was not a consistent thread.
Now what I want is a consistent thread that is always changing the text of my Textview. I tried using this, but didn't work, any help?
private void startClock() {
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
while (true) {
final long millis = System.currentTimeMillis() - MainActivity.startedAt;
clock.setText("" + millis);
runOnUiThread (new Runnable() {
#Override
public void run() {
clock.setText("" + millis);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}, 2000);
}
you should get rid of:
while(true) {
....
sleep(1000);
...
}
because this get your thread stuck forever. your program should work like this:
private Handler mHandler = new Handler();
#Override
public void onResume() {
super.onResume();
mHandler.removeCallbacks(mUpdateClockTask);
mHandler.postDelayed(mUpdateCLockTask, 100);
}
#Override
public void onPause() {
super.onPause();
mHandler.removeCallbacks(mUpdateClockTask);
}
private Runnable mUpdateClockTask = new Runnable() {
public void run() {
updateClock();
mHandler.postDelayed(mUpdateClockTask, 2000);
}
};
and inside updateClock() you do all your UI updates.
Look here for an example https://stackoverflow.com/a/11140429/808940
Also note that you have a duplicate line in your code:
clock.setText(""+millis);
It appears both in the runOnUiThread and in the main handler, it should only appear in the runOnUiThread runnable

Categories

Resources