I'm using a pretty standard loop with sleep that increments by 100 milliseconds each time, but I want to add a text color change halfway through... However, I get an error. Is there a problem in the following code?
public class splashActivity extends Activity {
TextView tv;
LinearLayout ll;
protected boolean _active=true;
protected int _splashTime=5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
tv=(TextView)findViewById(R.id.textViewSplash);
ll=(LinearLayout)findViewById(R.id.linearLayoutSplash);
Thread splashThread = new Thread(){
#Override
public void run() {
try{
int waited=0;
while (_active && waited < _splashTime){
sleep(100);
if(_active){
waited += 100;
}
if(waited >=2500){
tv.setTextColor(Color.GREEN);
}
}
}
catch(InterruptedException e){
}
finally{
finish();
startActivity(new Intent("com.kleaverdevelopment.splashTest.SplashTest.mainActivity")); //package.package.package.appName.nextActivity
stop();
}
}
};
splashThread.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
_active = false;
}
return true;
}
}
You must not update UI elements (TextView in your case) from a background thread. See the article about UI and threading on Android.
Related
I write a Splash Screeen to run at the boot time of application
public class SplashScreen extends Activity {
ImageView imgView;
int[] imgID = new int[]{R.drawable.frame0, R.drawable.frame1, R.drawable.frame2, R.drawable.frame3,
R.drawable.frame4, R.drawable.frame5, R.drawable.frame6};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
imgView = (ImageView) findViewById(R.id.imgSplash);
new Thread(new WelcomeScreen()).start();
}
private class WelcomeScreen implements Runnable {
#Override
public void run() {
try {
for (int i = 0; i < imgID.length; i++)
{
imgView.setImageResource(imgID[i]);
sleep(500);
}
} catch (InterruptedException e) {
}finally {
Intent intent = new Intent(SplashScreen.this,LoginActivity.class);
startActivity(intent);
finish();
}
}
}
}
It getting error "Sorry the application has stopped unexpectedly" . I don't know why . Somebody can help me ????
you can not set the resource for yuor ImageView inside a thread different from the UI Thread.
you can use runOnUiThread. It takes as paramter a runnable, and post it in the UI Thread queue. There, the UI thead takes it and update your ImageView. All in all your runnable will become:
private class WelcomeScreen implements Runnable {
#Override
public void run() {
try {
for (int i = 0; i < imgID.length; i++)
{
final int resuorceId = imgID[i];
runOnUiThread(new Runnable() {
#Override
public void run() {
imgView.setImageResource(resuorceId);
}
});
sleep(500);
}
} catch (InterruptedException e) {
}finally {
Intent intent = new Intent(SplashScreen.this,LoginActivity.class);
startActivity(intent);
finish();
}
}
You can not access your views from Thread.
You will need to put your code imgView.setImageResource(imgID[i]); in runOnUiThread
use like:
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
imgView.setImageResource(imgID[i]);
}
});
Thanks
You can not change something in UI from non-UI thread so replace this you code:
imgView.setImageResource(imgID[i]);
to:
runOnUiThread(new Runnable() {
#Override
public void run() {
imgView.setImageResource(imgID[i]);
}
});
//try code this way...
public class SplashScreen extends Activity {
private Intent launchIntent;
private Thread splashThread; //used for perform splash screen operation
private int splashTime = 10000, sleepTime = 50; //used for threading operation
private boolean active = true; //used for touch event
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen); //Set splashscreen.xml here
try {
splashThread = new Thread() { // Creating Thread for splash the screen
#Override
public void run() { // run method implemented to perform threading operation
try {
int waitTime = 0; //counter for threading
do {
sleep(sleepTime); //delay for specific time
if (active)
waitTime += 100;
//write your image code here that display your no. of images
} while (active && (waitTime < splashTime)); //Check touch condition and counter
} catch (Exception e) {
// to handle runtime error of run method
Validation.displayToastMessage(SplashScreen.this, e.toString()); //Call static method of class ToastMessage
}
finish(); //finish current activity
startJustCoupleActivityScreen(); //Call below defined function
}
};
splashThread.start(); //start thread here
} catch (Exception e) {
message("SplashScreen : "+ e.toString()); //Call static method of class ToastMessage
}
}
public void startJustCoupleActivityScreen() {
launchIntent=new Intent(SplashScreen.this,JustCoupleActivity.class); //call Next Screen
startActivity(launchIntent); //start new activity
}
#Override
public boolean onTouchEvent(MotionEvent event) { //onTouch Event
//on touch it immediate skip splash screen
if(event.getAction()==MotionEvent.ACTION_DOWN) active=false; //Check Touch happened or not
return true;
}
public void message(String msg)
{
Validation.displayToastMessage(SplashScreen.this, msg); //display Error Message
}
}
My splash screen gets error and it is going to force close the app. in Logcat is says Permission Denied. What should i do to fix this problem. Can anyone help me with that matter, Thanks a lot
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
public class SplashScreen extends Activity {
protected boolean _active = true;
protected int _splashTime = 4000;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
// thread for displaying the SplashScreen
Thread splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime)) {
sleep(100);
if(_active) {
waited += 100;
}
}
} catch(InterruptedException e) {
// do nothing
} finally {
finish();
startActivity(new Intent("com.droidnova.android.splashscreen.BodyPartsGameActivity"));
stop();
}
}
};
splashTread.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
_active = false;
}
return true;
}
}
do this it will easier to you:
private ImageView img;
private Thread t;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView)findViewById(R.id.im1);
t = new Thread(){
#Override
public void run() {
try {
synchronized(this){
wait(2000);
}
// Here your code of animation
}
catch(InterruptedException ex){
ex.printStackTrace();
}
Intent i = new Intent(QuizActivity.this,main.class);
startActivity(i);
}
};
t.start();
}
Your Problem is that you don't add the QuizActivity to the AndroidMainfest.xml
<activity android:name="QuizActivity" ></activity>
than your problem should be solved.
and befor you start the new Activity in splash make
finish();
than you cant go back with back key to it =)
Can you try this once..
Thread splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime)) {
sleep(100);
if(_active) {
waited += 100;
}
}
} catch(InterruptedException e) {
// do nothing
} finally {
runOnUiThread(new Runnable() {
#Override
public void run() {
finish();
startActivity(new Intent("com.droidnova.android.splashscreen.BodyPartsGameActivity"));
stop();
}
});
}
}
};
So in my further attempts to implement a while loop in android I have come up with the code below :
private boolean connected = false;
private Thread loop = new Thread() {
public void run() {
Looper.prepare();
while (connected) {
do_Something();
}
Looper.loop();
}
};
onCreate() {
//.....
ok = (Button) findViewById(R.id.button1);
ok.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (connected) {
try {
loop.start();
}
catch (Exception e) {
Toast.makeText(getBaseContext(), "Exception caught", Toast.LENGTH_LONG).show();
}
}
return true;
}
});
stop = (Button)findViewById(R.id.button2);
stop.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//disconnects current connection
connected = false;
}
return true;
}
});
}
That is, I am trying to on the touch of the first button have my UI thread switch to the thread that will do_something over and over again until the touch of the second button, in which case the boolean var k will switch off and completely stop the newly created thread from the button press of the first button. I have google'd "threads/handlers/android while loops" but to no avail. Any help towards what I am trying to do would be much appreciated
Simply put, how do I kill the thread that was created via pressing the second button?
Did you tried AsyncTask ? What you can do...start a new AsyncTask on firstButton click and cancel it on secondButton click.
//define global variable
private DoSomething doSomething;
//register firstButton onClickListener
firstButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//start your asynctask
if(doSomething == null || doSomething.isCancelled()){
doSomething = new DoSomething();
doSomething = doSomething.execute();
}
}
});
//register secondButton onClickListener
secondButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
doSomething.cancel();
}
});
//Inner AsyncTask class
class DoSomething extends AsyncTask<Void, Void, Void>{
#Override
protected Void doInBackground(Void... params) {
//doSomething();
while(true){
System.out.println(1);
if(isCancelled()){
break;
}
}
return null;
}
}
Note: This is pseudocode...might contain error...just want to give you overview. hope this help.
Is it possible to set a default image during starting an application? On iPhone, I can set an image named "Default.png" to do that. But is it possible on Android?
(For information I ask that because I can't use a splascreen view or auto-activity to an other, because of videoview in the first activity.)
I dont understand a reason why not to use splashscrean
public class SplashScreenActivity extends Activity {
protected boolean _active = true;
private Thread splashTread;
protected int _splashTime = 2000; // time to display the splash screen in ms
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
// thread for displaying the SplashScreen
final SplashScreenActivity sPlashScreen = this;
// thread for displaying the SplashScreen
splashTread = new Thread() {
#Override
public void run() {
try {
synchronized (this) {
// wait 5 sec
wait(_splashTime);
}
} catch (InterruptedException e) {
} catch (UnsupportedOperationException e1) {
// TODO: handle exception
} finally {
finish();
// start a new activity
Intent i = new Intent();
i.setClass(sPlashScreen, Your videoViewActivity.class);
startActivity(i);
}
}
};
splashTread.start();
}
// Function that will handle the touch
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
synchronized (splashTread) {
splashTread.notifyAll();
}
}
return true;
}
}
i doing simple stop watch with [start/pause] and [reset] button. problem occur when i pressed start button after pause. the run method is not calling. please help me.
my code is
public class StopWatch3 extends Activity implements Runnable{
// text view influenced by the Thread
private TextView threadModifiedText;
int time=0;
Button b1,b2,b3;
boolean shouldRun = false;
/** Called when the activity is first created. */
Thread currentThread = new Thread(this);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stopwatch);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
threadModifiedText = (TextView) findViewById(R.id.textView1);
Log.e("before",""+currentThread.getState());
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Log.e("stopw",(String) b1.getText());
if(b1.getText().toString().equals("start")){
if(currentThread.getState()==Thread.State.NEW){
currentThread.start();
Log.e("after",""+currentThread.getState());
shouldRun = true;
b1.setText("pause");
}
else{
shouldRun = true;
b1.setText("pause");
}
}
else if(b1.getText().toString().equals("pause")){
shouldRun = false;
b1.setText("start");
}
}
});
b2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
time=0;
}
});
}
#Override
public void run(){
try {
while(shouldRun){
Thread.sleep(1000);
threadHandler.sendEmptyMessage(0);
}
} catch (InterruptedException e) {
}
}
private Handler threadHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
time++;
threadModifiedText.setText(""+time);
}
};
}
You cannot start thread after it finishes it's job, and his state is no longer NEW anyway, you'll have to create a new thread in this case.
When you press "start" the second time, you reach this part:
else{
shouldRun = true;
b1.setText("pause");
}
And nothing in this code will make the thread run again of course...
I think your thread just runs to an end when you set shouldRun to false.
Enclose your while loop into another while loop that is true as long as your program runs.