Displaying new screen takes too much time! (threads maybe...?) - java

TL;DR: My intent takes too much time to start from a different thread, whereas starting from the main thread is very fast. I dont actually know if this is a problem with threads or with the onFinish method
So, I have a countdown timer. It counts down from twenty seconds, and onFinish() I have an intent. I am also periodically setting the text of my textView based on millisUntilFinished. I noticed, that after the textView says 1 second left, the intent starts after 3 seconds.
But, if I am switching activities by using an intent from OUTSIDE of the onFinish method the next activity starts quickly. So,
Why does starting an intent from a the onFinish method take longer than usual?
According to my little test with my timer, I decided that I need a better and faster way to start my intent, since clearly, the onFinish method launches after more time then the timer actually starts. So, what should I do to start my intent faster? I need it to be immediate...
public void startTimer() {
timer = new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) millisUntilFinished/1000;
timetext.setText(seconds + ":00");
}
public void onFinish() {
Intent intent = new Intent(MainActivity.this, GameOver.class);
intent.putExtra("score", score); // pass your values and retrieve them in the other Activity using keyName
intent.putExtra("classname", "com.example.ruchir.swapproperties.MainActivity");
startActivity(intent);
}
}.start();
}
Thanks,
Ruchir

Try placing the startActivity(intent) outside of the timer as such:
public void startTimer() {
timer = new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) millisUntilFinished/1000;
timetext.setText(seconds + ":00");
}
public void onFinish() {
Intent intent = new Intent(MainActivity.this, GameOver.class);
intent.putExtra("score", score); // pass your values and retrieve them in the other Activity using keyName
intent.putExtra("classname", "com.example.ruchir.swapproperties.MainActivity");
}
}.start();
startActivity(intent);
}

You can have a try :
public void onFinish() {
cancel(); //Cancel the countdown
Intent intent = new Intent(MainActivity.this, GameOver.class);
intent.putExtra("score", score); // pass your values and retrieve them in the other Activity using keyName
intent.putExtra("classname", "com.example.ruchir.swapproperties.MainActivity");
startActivity(intent);
}
`

Related

Passing countdown timer value to next activity

i'm trying to pass the countdown timer value as textview to next activity, but i don't know how, do i need do use intent.putextra() ?
There is my code:
countDownTimer = new CountDownTimer(5000, 1000) {
#SuppressLint("DefaultLocale")
public void onTick(long millisUntilFinished) {
timpRamas.setText(String.format("%d:%d",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
Intent intent = new Intent(c1_1.this,TimpExpirat.class);
startActivity(intent);
finish();
}
}.start();
You must pass the data of the textView to the next activity and in the next activity you can set the data in another view or do whatever with it:
public void onFinish() {
Intent intent = new Intent(c1_1.this,TimpExpirat.class);
intent.putExtra("data" , timpRamas.getText().toString());
startActivity(intent);
finish();
}
In the TimpExpirat activity:
//get the data in onCreate()
Intent intent = getIntent();
String time = intent.getStringExtra("data");
//time now has the time that was last set on your textview, you can set it
//to a new textview or do whatever with it.

How can I store the result of and if else statement to use on another page of an android app

I'm looking to store the result of a stopwatch timer when I click the stop button, then place this result in a table on the second page of my an android app. The stopwatch works as planned, but how to I store the result?
mButtonStartPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!running) {
chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
chronometer.start();
running = true;
mButtonStartPause.setText("Pause");
} else {
pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
chronometer.stop();
running = false;
mButtonStartPause.setText("Start");
}
}
});
You'll want to pass your data through the Intent.
Intent intent = new Intent(this, ResultActivity.class);
intent.putExtra("STOP_WATCH_TIME", "8:03"); // Pass whatever data you want.
startActivity(intent);
And then within your ResultActivity, you can get that data.
String time = getIntent().getStringExtra("STOP_WATCH_TIME");

How to trigger or send intent continously (without on button click)

I am tryiny to update my edittext in UI. I have a service from where I send a intent by clicking a button and update in the UI. Everything works fine. But I would like to send intent without button click.
What I tried was to put my Intent in a method and call it oncreate in service but even then it been just called once.
public class myService extends service {
onCreate{
sendMessage();
}
private void sendMessage(){
Intent intent = new Intent("com.example.app.SEND");
intent.putExtra("KEY", (String) Number);
sendBroadcast(intent);
}
When I do like this, I just send empty string which is not useful. And even then just once.
Can I send intent continously ? So that It updates my UI once it receives input ? Any possible way to do it ?
I would like to send intent every 5 seconds.
You can use TimerTask with IntentService for scheduled jobs.
ex:
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
//some execution
}
};
timer.schedule(timerTask, 5000, 5000);

How to stop spending broadcast in Android?

I have an Android project which is sending a Broadcast every second and am trying to figure out how to stop it after a click.
My broadcast code is:
Intent broadcastIntent = new Intent ("send broadcast");
sendBroadcast(broadcastIntent);
stoptimertask(); //it is stopping broadcast for a second.
You can define two methods: one that start a Timer to send a broadcast every second and a second one that stop the Timer.
Timer timer;
private void startBroadcastLoop() {
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
// Send broadcast
Intent broadcastIntent = new Intent ("send broadcast");
sendBroadcast(broadcastIntent);
}
},0,1000); // Send broadcast every second
}
private void stopBroadcastLoop() {
if(timer!=null){
timer.cancel();
timer = null;
}
}
And then on your button, call the right function according to the state of a boolean:
sendBroadcastBool = false;
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// If broadcast not sent yet
if (!sendBroadcastBool) {
startBroadcastLoop();
sendBroadcastBool = true;
}
else {
stopBroadcastLoop();
sendBroadcastBool = false;
}
}
});
Best

Android-Starting new Activity in Handler/runnable is really slow

I am making an android app that requires a runnable. I am starting a new activity from the runnable. The new activity comes up and works fine. The issue is that when the call is made to start the activity, it is incredibly slow. It takes a full 5 seconds to start the activity when I want it to be instantaneous.
Boolean handlerrun=true;
Intent intent= new Intent(this,newactivity.class);
int somevalue=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameactivity);
handler=new Handler();
final Runnable r = new Runnable()
{
public void run()
{
if(handlerrun){somevalue++;}
if(somevalue>500){
handlerrun=false;
startActivity(intent);
finish();
}
handler.postDelayed(this, 1);}
}
};
handler.postDelayed(r, 1);
}
The activity starts when somevalue is greater than 500. To stop the handler from increasing the value of somevalue, I use a boolean handlerrun, which only runs the handler when it is true. When somevalue is greater than 500, handlerrun= false so the handler doesn't increase the value. I tried using the handler.removeCallbacksandMessages() method but it didn't work. Logcat doesn't give me any errors.Any help would be appreciated.
You could try something like this:
#Override
protected void onResume() {
super.onResume();
if(done){
return;
}
done = true;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(getApplicationContext(), YourActivity.class));
finish();
overridePendingTransition(0, 0);
}
}, 5000);
}
That will start YourActivity after 5 seconds approximately.
Hope it helps.

Categories

Resources