Android countup timer for all activities - android

I'm making an application contains about 20 activities and I want to start a count up timer when the activity 1 starts and finish counting on the last activity. I've found a way to make a subclass and call the timer in 1 activity But I didn't know how to pass the value of the timer from activity 1 to activity 2 and from 2 to 3 . this is my code
subclass
package com.mytimer;
import java.lang.ref.WeakReference;
import java.util.TimerTask;
import android.os.Handler;
import android.widget.TextView;
public class IncrementTask extends TimerTask {
WeakReference<TextView> mRef;
int counter = 0;
Handler handler = new Handler();
public IncrementTask(TextView text) {
mRef = new WeakReference<TextView>(text);
}
public void run() {
handler.post(new Runnable() {
public void run() {
mRef.get().setText("counter " + counter);
counter++;
}
});
}
}
in my activity 1
TextView mTextView = (TextView)findViewById(R.id.text);
Timer timer = new Timer();
IncrementTask task = new IncrementTask(mTextView);
timer.scheduleAtFixedRate(task, 0, 1000);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent (MainActivity.this, Page2.class);
startActivity(i);
}
});
I want to know how to pass the value of the timer to the next activity not start the timer from 0
any help please

In my opinion use Fragments instead of Activity because if you use fragments it is able to show two fragment on screen which contains timer and your remaining content.
If you use activity it is not possible to show accurate timer values while you move from once screen to other screen.

How do you start the subsequent activities? I suppose with startActivity() or startActivivtForResult() , if this is the case you cold pass the time in the intent.
But it is not a good solution because in the process you would "lose time" because the activity staring is not exact to the second.
Why not create a timer activity that starts at the end of the process and finishes at the end?

Related

change activity inside handler

I want to change from my current activity to another while I am inside a Handler. The idea is other code inside the handler will run until a certain condition doesnt match (with increment of the count value every time). When the count value matches the condition I close the activity and move to another.
my code is:
mHandler = new Handler();
mRunnable = new Runnable() {
#Override
public void run() {
if(count<CONDITION_VALUE)
{
//do other stuff...
count++;
}else
{
//change activity...
finish();
}
mHandler.postDelayed(mRunnable, 4000);
}
};
mHandler.postDelayed(mRunnable, (1000));
The code is running without any error but the old activity is not being destroyed (i guess) and the new activity is reloaded after every 4 seconds.
I want the new activity to load only once.
How can I achieve this?
try this, from enter link description here
in your Activity
#Override
public void onDestroy() {
mHandler.removeCallbacks(mRunnable);
super.onDestroy();
}

Auto Redirect When User Is Taking Too Long Input In Activity Android

I have a scenario, which when a User Rating, or inputting on a data, then the current Activity will Time it to the setted time.
So, if the User isn't do Anything, or taking the Action to Long, then the current Activity will direct the User into the MainActivity.
In my case, i have a Rating app, which is located in a Public place. I thought that if People wants to Rate BUT not completing the Quiz phase, then i don't want to leave the last Quiz to meet the new People who wants to Rate.
I've tried using these code:
int timeout = 4000;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
Intent homepage = new Intent(Quiz2.this, MainActivity.class);
startActivity(homepage);
finish();
}
}, timeout);
And these one:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(Quiz2.this, MainActivity.class));
}
}, 4000);
It works, but it didn't work as expected, as it apply to ALL of the activities (I mean, after these code works in Current Activity, the Rest of the Activities is Applied and Timed too)
I don't want this. What i want is to Apply these Timer ONLY in Current Activity.
How this can be done?
Appreciate for any help, Regards.
I didn't fully understand what you actually wanna do,
but I'm guessing using CountDownTimer and then starting the other activity when the timer finished should do the trick.
new CountDownTimer(4000, 1000) {
public void onTick(long millisUntilFinished) {
// You could show the user the time left using `millisUntilFinished`
}
public void onFinish() {
// Do something when the timer is finished (start your activity & finish)
}
}.start();

Using intents in android is causing problems between activities

I have run into a problem. I have an animation that is going in my first activity, and a countdown timer in my second activity. When I set the countdown timer the value is passed back into my first activity which begins a new countdown activity, but the animation stops. How can I pass the value of the countdown timer to my MainActivity while still Maintaining the animation in my main activity.
Here is my code for the intent in my SecondActivity that is being passed to my MainActivity. Im not sure what code to show because I am not sure what the cause of the problem is.
buttonStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//put your logic here to set the time with hourPicked and minPicked variables
timer = new CounterClass((hours * 60 * 1000) + (minutes * 1000), 1000);
String hms = String.format(("%02d:%02d"), hours, minutes);
textViewTime.setText(hms);
Intent intent = new Intent(SleepTimer.this, MainActivity.class);
String TimeValue = (hms);
intent.putExtra("TimeValue", TimeValue);
startActivity(intent);
timer.start();
}
});
You have to declare String hms globally and onclick of button finish the SleeoTimer activity.and access global variable hms at MainActivity in SleepTimer Activity create field as public static String hms;
and in MainActivity use it as SleepTimer.hms
buttonStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//put your logic here to set the time with hourPicked and minPicked variables
timer = new CounterClass((hours * 60 * 1000) + (minutes * 1000), 1000);
hms = String.format(("%02d:%02d"), hours, minutes);
textViewTime.setText(hms);
SleepTimer.this.finish();
}
});
You have to add your animation code on Activity Oncreate,OnPause and also in OnResume as per your requirement.

Image capture in Android automatically

Is there any way to use the following code to take pictures automatically i.e. no button clicks at all. Just after sometime image can be taken automatically and stored on the SD card.
protected void startCameraActivity() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(file_name)));
startActivityForResult(intent, 1);
finish();
}
No, once you start an Intent you really have no control over the Activity you start (assuming it's not one that you wrote yourself). In your case, you have to make your own Activity and use the Camera API.
Check out this tutorial:
http://marakana.com/forums/android/examples/39.html
You can use Timer & TimerTask Class together for your requirement. Just study the following code and modify it according to your usage.
import java.util.Timer;
import java.util.TimerTask;
class MyTimerTask extends TimerTask
{
public void run()
{
// Put your camera capturing and photo saving code here
}
}
public class MainClass
{
public static void main(String args[])
{
MyTimerTask myTask = new MyTimerTask();
Timer myTimer = new Timer();
/*
* Set an initial delay of 15 second, then repeat every 10 second.
*/
myTimer.schedule(myTask, 15000, 1000);
}
}

How can I make the android program display a layout and then wai a few sec. and the display another layout?

Hi guys I just want the program to display the layout main0 and stay for a few secounds then display layout main1 like the programs we see in any phone where an image or layout show up at the start of the program and then fade.
/**the main activity */
public class rdwt extends Activity implements OnClickListener{
Button b1;
Button b2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main0);
//Here
setContentView(R.layout.main1);
b1= (Button)findViewById(R.id.Button01);
b2= (Button)findViewById(R.id.Button02);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
public void onClick(View v) {
if (v==this.b1){
Intent callwrite = new Intent(this, wto.class);
startActivity(callwrite);
}
if(v==this.b2){
Intent callread = new Intent(this, rfr.class);
startActivity(callread);
}
}
}
You need to read a technical article about Updating the UI from a Timer
I think there are particularly two different approaches.
1) use a android.os.Handler and send a delayed message
2) use a timer and a timer task to update your layout after a specific amount of time
Code example:
TimerTask bla = new YourTask();
Timer timer = new Timer();
timer.schedule(bla, 1000);
And your class YourTask:
public class YourTask extends TimerTask {
public void run() {
updateYourLayout();
}
}
Edit: However I think you're looking for a splashscreen. This simple tutorial explains how to do it: http://www.anddev.org/simple_splash_screen-t811.html
// Place the following line in your code.
timer(3500); // Waits 3.5 seconds before moving on to the next activity.
public void timer(int counter){
new Handler().postDelayed(new Runnable(){
#Override
// counter is in milliseconds.
public void run() {
Intent mainIntent = new Intent(THISCLASSNAME.this,CLASSNAMETOJUMPTO.class);
startActivity(mainIntent);
finish();

Categories

Resources