How to prevent multiple ScheduledExecutorServices from being created upon reopening activity - java

My issue is that upon opening a certain activity in my project, I initialize a ScheduledExecutorService that sends an Intent to an IntentService class every 20 seconds.
Now when I first open the activity that contains the ScheduledExecutorService, the Intent fires once every 20 seconds as planned.
The issue arises when I exit the activity (staying in the app) and then reenter the activity. This results in the Intent being sent twice in a 20 second window, and I figure it has to do with my creating a new ScheduledExecutorService in the onResume of my activity.
How do I ensure that there is only one instance of ScheduledExecutorService at any given time?
Relevant code is below:
#Override
public void onResume() {
super.onResume();
ScheduledExecutorService scheduleIntentSender = Executors.newScheduledThreadPool(1);
scheduleIntentSender.scheduleAtFixedRate(new Runnable() {
public void run() {
sendIntent();
}
}, 0, 20,TimeUnit.SECONDS);
mDownloadStateReceiver =
new DownloadStateReceiver();
// Registers the DownloadStateReceiver and its intent filters
LocalBroadcastManager.getInstance(this).registerReceiver(
mDownloadStateReceiver,
testIntentFilter);
}

I suggest not doing that in your Activity because it's intended to display a UI. Do that in a Service instead. You can launch a Service in onStart and track the state of the executor in your Service, whether it's launched or not. Service is good because it's a background component which is not tied to UI at all. It will not be affected during screen rotations etc.

You should cancel the previous ScheduledExecutorService after closing activity:
ScheduledExecutorService scheduleIntentSender = Executors.newScheduledThreadPool(1);
final ScheduledFuture schedulHandler = scheduleIntentSender.scheduleAtFixedRate(new Runnable() {
public void run() {
sendIntent();
}
}, 0, 20,TimeUnit.SECONDS);
//Call schedulHandler.cancel(true) to cancel scheduleIntentSender in onDestroy()

Related

How to set a timer that do something after it finishes in an activity with button that if clicked something else happens?

I have an activity in my android app that shows a word to the user. if the user could guess the answer in less than 60 seconds he would press a button and go to another activity. but if he could not do it and the time finishes, a third activity must show. How should I do it? with threading or timer or something like that?
I have tried threading but the app crashes.
You can achieve this by using Handler.
Kotlin
// declare this variables as attributes in you class
val handler = Handler()
val runnable = Runnable {
// Call something when it finishes
}
handler.postDelayed(runnable, 60_000) // Do something after 60 seconds
// and if you want to cancel the timer, you can cancel it this way
handler.removeCallbacks(runnable)
Java
// declare this variables as attributes in you class
Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
// Call something when it finishes
}
};
handler.postDelayed(runnable, 60_000);
// and if you want to cancel the timer, you can cancel it this way
handler.removeCallbacks(runnable);

Android Timer in Background Service not updating on time

I am trying to update a user location to our REST WebService every minute. I have tried using a Timer and a Runnable with Handler. Both of these are having problems.
Both work fine on my Emulator, although on our test devices the timer and Runnable are not running every minute, they update sporadically, sometimes within 1 second of each other, sometimes 5 minutes later, and anywhere in between..
Does anyone know why this might happen?
Context context;
Handler handler = new Handler();
#Override
public void onCreate() {
super.onCreate();
context = this;
handler.postDelayed(runnable, 0);
}
private Runnable runnable = new Runnable() {
#Override
public void run() {
updateLocation(context);
handler.postDelayed(this, 60000);
}
};

Android start/stop repeating Thread after specific time

I need a thread repeating after 30 seconds that load data from web service.
i want to stop and start thread onResume and onPause so that it runs when screen is active and disable it when activity paused. Restart on resume.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
dataIfInternet(refreshTop);
}
}, 30000);
How to stop it when activity paused.
U can't pause the handler, so u need to cancel it in your onPause and post in again in your onResume
so u probably want something like this:
private Handler myHandler = new Handler();
in your onResume()
Runnable myRunnable = new Runnable()
{
#Override
public void run()
{
dataIfInternet(refreshTop);
}
};
myHandler.postDelayed(myRunnable, 30000);
and in your onPause()
// Cancel the runnable
myHandler.removeCallbacks(myRunnable);
I hope this solves your problem

Android - Activity finish() results black screen

I have an AlertActivity and an Activity. When a broadcast is received, both activities needs to finish. But the below code results Black screen if AlertActivity is on top of Activity.
Below is the code in Activity:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("BROADCAST_INTENT")){
if(alertActvity != null)
alertActivity.finish();
finish();
}
}
And code in AlertActivity:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("BROADCAST_INTENT"))
finish();
}
}
First, Activity's onStop() is getting called before AlertActivity's onStop() is called which results in Black screen, even AlertActivity's finish() called before Activity's finish().
Please help me in this regard.
Finally, I found a solution for this:
Finishing an Activity with a delay of 1 second which really works. By that time, AlertActivity finishes and black screen cannot be displayed.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
finish();
}
}, 1000);
as in both AlertActivity and Activity, you are checking for same action "BROADCAST_INTENT", I assume you registered both receiver in their own class.
If you did so, then actually you have two broadcast receiver waiting for same event. when this event occurs, both of your receiver are going to get it.
So in your AlertActivity is getting finished twice.
I think #Amit K. Saha, is right, your AlertActivity may be finishing twice
Solution :
If your application is running >= API 16 then you can use finishAffinity() method :
Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch.
Note that this finish does not allow you to deliver results to the
previous activity, and an exception will be thrown if you are trying
to do so.
You can call finishAffinity() from AlertActivity because it is on top of Activity. This will finish AlertActivity as well as Activity
My transparent Activity finish results black screen, after a search, i find it is caused by activity switching animation in Android 4.4. But above android 5.1 the phenomenon does not show up.
So I add the below code:
#Override
public void finish() {
super.finish();
overridePendingTransition(0, 0);
}
The black screen after finish is gone.
I think this may be helpful.
try this
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("BROADCAST_INTENT"))
context.finish();
}
}

Android Thread Allocation - growing heap?

Hi everyone out there,
i am developing an android application against API 7 at the moment in which i use an activity which need to be restarted. Lets say my activity looks like this:
public class AllocActivity extends Activity implements OnClickListener{
Button but;
private Handler hand = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_alloc);
but = (Button) findViewById(R.id.button);
but.setText("RELOAD");
but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0){
Intent intent = getIntent();
startActivity(intent);
finish();
}
});
}
#Override
protected void onDestroy(){
super.onDestroy();
System.gc();
}
/****** THREADS AND RUNNABLES ******/
final Runnable fullAnim = new Thread(new Runnable(){
#Override
public void run(){
try{
hand.post(anim1);
Thread.sleep(2000);
hand.post(anim2);
Thread.sleep(1000);
// and so on
}catch(InterruptedException ie){ie.printStackTrace();}
}
});
final Runnable anim1 = new Runnable() {
#Override
public void run(){
// non-static method findViewById
ImageView sky = (ImageView)findViewById(R.id.sky);
}
};
}
The problem is that the gc doesnt seem to free the fullAnim thread so that the heap is growing by ~100K at every restart - till it slows down and crashes. Declaring fullAnim as static does solve this problem - but as i use non static references this doesnt work out for me.
So at this point i am kindof lost - and i hope u can advice me where to go next. Is there something i might be doing wrong or is there a tool i can use to manage threads to drop and free heap after restart.
kindly regards
UPDATE
thanks to everyone who answered - helped alot. using TimerTask did the trick in the end. i did the following change:
/****** THREADS AND RUNNABLES ******/
final TimerTask fullAnim = new TimerTask(){
#Override
public void run(){
try{
hand.post(anim1);
Thread.sleep(2000);
hand.post(anim2);
Thread.sleep(1000);
// and so on
}catch(InterruptedException ie){ie.printStackTrace();}
}
};
as the activity was more than 6k loc long this was a pretty decent solution without facing bigger impacts. KUDOS!
i dont use a Timer to shedule the task - dont know if its bad practice but
the animation is called like this:
Thread t = new Thread(fullAnim);
t.start();
A running Thread is never garbage collected.
A Thread is not stopped automatically if your Activity stops or is destroyed. It could run forever.
Every non-static inner class keeps a reference to the enclosing instance. E.g. hand.post(anim1); works inside that inner class because it has an implicit reference to AllocActivity.this.
So what you effectively do is to keep a reference to your Activity alive for longer than it is supposed to be alive, i.e. until after onDestroy.
Make sure to stop threads manually if you don't want them anymore.
Because final variable have low priority for GC. So you need to explicitly release the runneable objects in onPause() method because there is not ensurence onDestory() will call immediate after finish() call .
#Override
protected void onPause(){
super.onPause();
//cancel timer to stop animations
if(t!=null){
t.cancel();
}
System.gc();
}
UPDATE
use timer to achieve this
boolean isFirstAnim=true;
Timer t = new Timer();
t.schedule(new TimerTask() {
#Override
public void run() {
if(isFirstAnim){
// play your first animation at every
}else{
// play your second animation at every
}
}
}, 0, 3000);
What happens when all activities of an application finishes?
"When you call finish() this doesn't mean the Activity instance is
garbage collected. You're telling Android you want to close the
Activity (do not show it anymore). It will still be present until
Android decides to kill the process (and thus terminate the DVM) or
the instance is garbage-collected."
You need to implement your own stop method to stop the running thread, you can make a call to it in onDestroy
refer this Stopping a runnable
Alternatively
you can perform your operation in an asynctask and use onProgressUpdate() to publish progress on UI thread and use cancel(true) in combination with check in doInBackground() whether cancel has been called to stop the task.

Categories