Timer() function not working when going to another activity - java

In my application i want to close it after 5 seconds using Timer() function.It works when i am in MainActivity but when i go to another activity then the application do not close.Now how to run this Timer() function in background if i switch activity.What to do in this case?
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
finish();
}
}, 5000); // Application will be closed after 5 seconds

You achieve this using broadcast receiver. in your activity which you want to finish you need to create broadcast receiver.
public class TestActivity extends Activity {
public static String intent_filter_finish = "com.test.finish";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
registerReceiver(finishReceiver,
new IntentFilter(intent_filter_finish));
}
#Override
protected void onDestroy() {
unregisterReceiver(finishReceiver);
super.onDestroy();
}
BroadcastReceiver finishReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
}
now in your second activity you need to send broadcast after 5 second e.g.
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
sendBroadcast(new Intent(TestActivity.intent_filter_finish));
}
}, 5000);
}
}
or other possible way is directly use postDelayed() method in your test activity e.g.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
finish();
}
}, 5000);

Related

Is there a way to refresh webView every x seconds?

Now I have this function for reload webView:
public void reloadWebView() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
webView.reload();
}
}, 5000);}
And it is called on onCreate function
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
reloadWebView();
}
The function works but it only runs once
Easy to solve as long as the Activity is running in foreground:
Handler handler = new Handler();
public void reloadWebView() {
handler.postDelayed(new Runnable() {
#Override
public void run() {
webView.reload();
reloadWebView();
}
}, 5000);}
Note that handler is now a field, out of reloadWebView().
Now call it in your onCreate():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
reloadWebView();
}
Here is code to update your webview after x seconds
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
webView.reload();
//here you can have your logic to reload webview
}
public void onFinish() {
// hide progress bar if any
}
}.start();

android App is launching automatically after minimizing app

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"OnCreate()");
setContentView(R.layout.activity_main);
mToolbar = findViewById(R.id.my_toolbar);
mTextview = findViewById(R.id.title);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragment, new OneFragment());
//ft.commit();
ft.addToBackStack("frag");
ft.commitAllowingStateLoss();
}
}, 5000);
}
after minimizing my android app again its opening automatically.please help me to solve this problem
The Correct way to manage the handler is,
In your activity add this code,
private void initTasks() {
handler = new Handler();
runnable = new Runnable() {
#Override
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
};
handler.postDelayed(runnable, 5000);
}
#Override
protected void onResume() {
super.onResume();
initTasks();
}
Then to stop opening your app after minimizing,
#Override
protected void onStop() {
super.onStop();
handler.removeCallbacks(null);
}

How to destroy previous activity when you jumped to next activity

I have activity A, having timer thread. after 5 sec it jumps to activity B via intent. how to destroy activity A when you are on activity B so that back button don't let you go back to activity A.
You have to clear the activity backstack.
intent = new Intent(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
StartActivity(intent);
just call finish() when you make the intent from A to B
Try this...
MainActivity.java:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Waiting(5);
}
public class Waiting {
Timer timer;
public Waiting(int seconds) {
timer = new Timer();
timer.schedule(new WaitingTask(), seconds * 1000);
}
class WaitingTask extends TimerTask {
#Override
public void run() {
System.out.println("Hi, I'm waiting here!");
Intent intent = new Intent(MainActivity.this,
MainActivity1.class);
MainActivity.this.finish();
startActivity(intent);
timer.cancel();
}
}
}
}
MainActivity1.java
public class MainActivity1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity1);
}
}

calling another activity function inside an activity via handler doesn't work

I want to start a CountDownTimer from another activity. I use handlers to do that, but it doesn't work. Which part am I doing wrong? This is my code:
activity1.java :
public class Activity1 extends Activity {
public static Handler mHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initHandler();
mHandler.sendEmptyMessage(1);
startActivity(new Intent(Activity1.this, Activity2.class));
}
private void initHandler(){
mHandler = new Handler(){
#Override
public void handleMessage(Message msg) {
switch (msg.arg1) {
case 1:
mCountDownTimer.start();
break;
}
}
};
}
private CountDownTimer mCountDownTimer = new CountDownTimer(10000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
Toast.makeText(Activity1.this, "Count is: "+ millisUntilFinished/1000, Toast.LENGTH_SHORT).show();
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
}
};
}
activity2.java :
public class Activity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Activity1.mHandler.sendEmptyMessage(1);
}
}
Why is this not working?
In your initHandler() method, change your condition in switch case from switch (msg.arg1) to switch (msg.what)
According to Android Developer site sendEmptyMessage(int) Sends a Message containing only the what value.

Accessing an activity through a service

I have an Activity and a service.
The activity has a TextView member and a setText() method.
I would like to call that method through the Service, how can I do that?
Here is the code:
Activity:
public class MainActivity extends Activity {
private TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.tv1 = (TextView) findViewById(R.id.textView1);
Intent intent = new Intent(this,MyService.class);
startService(intent);
}
// <-- some deleted methods.. -->
public void setText(String st) {
this.tv1.setText(st);
}
}
Service:
public class MyService extends Service {
private Timer timer;
private int counter;
public void onCreate() {
super.onCreate();
this.timer = new Timer();
this.counter = 0;
startService();
}
private void startService() {
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
//MainActivityInstance.setText(MyService.this.counter); somthing like that
MyService.this.counter++;
if(counter == 1000)
timer.cancel();
}
},0,100);
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
You can use an intent in order to send any information (i.e. the counter for TextView member) to the Activity.
public void run() {
//MainActivityInstance.setText(MyService.this.counter); somthing like that
MyService.this.counter++;
Intent intentBroadcast = new Intent("MainActivity");
intentBroadcast.putExtra("counter",MyService.this.counter);
sendBroadcast(intentBroadcast);
if(counter == 1000)
timer.cancel();
}
...then, you will receive your data in the Activity using a Broadcast Receiver
/**
* Declares Broadcast Reciver for recive location from Location Service
*/
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Get data from intent
serviceCounter = intent.getIntExtra("counter", 0);
// Change TextView
setText(String.valueOf(counterService));
}
};

Categories

Resources