I am implementing a timer in my application which checks an alarm all the time.This timer values am checking in my home page and showing alerts telling how much time left..How can i show these messages when i am in different Activities ?
One way would be to add your timer check code to a Fragment with no UI and use that Fragment in your various activities.
If your dialog box doesn't interact with your activity then you can start a TimerTask in a subclass of the Application object that checks your timers as often as you need. Then when a timer needs to go off, you can start a new activity that simply displays the alert dialog. Since you're starting it from the Application (which subclasses Context), you need to set the NEW_TASK flag and theme the activity to be a "dialog". You could even make the whole activity the dialog instead of starting the dialog in onStart()
Related
I have an application with several activities, and I have a timer I start in the first activity that is presented. The problem is this:
How can I get a reference to the current activity when the timer goes off when the activity I'm currently may not be the same as the one I started the timer.
What I actually want is to have a timer traverse all my actives, show an alert dialog when it expires and the do some stuff. But because of the way android works this seems to be impossible. Does anyone has an alternative?
I've already tried:
Using an async task to access the ui thread, doesn'nt work if it is not created in the main ui thread.
Can't use a Handler, my timer is in another class
What other choice do I have?
EDIT:
I canĀ“t change any of the activities code, the timer should be decoupled enough to function when someone plugs it in the project.
Getting an instance of the current activity from the timer worker thread should work, since it would let me run stuff in the ui thread.
Implement your timer as a singleton.
Then, implement an observer pattern:
Create an interface (maybe called AlertListener) that is implemented by each Activity you want to be alerted. This interface should have a method, something like onTimerExpired(). This method should do whatever needs to be done to the activity when the timer expires.
In your timer class, also maintain a reference to the current AlertListener and a method, named something like "setCurrentActivity(AlertListener currentActivity)".
In onResume or some other method of each activity, call MyTimer.setCurrentActivity(this).
When the timer goes off, call currentActivity.onTimerExpired().
I am designing an app that is used for emergency alerts. The alerts come from a server and a connection to that server is maintained in service.
If the service receives an emergency request from the server it checks to see if a specific activity is open. If it is it lets it know an emergency has been triggered and the activity launches a dialog activity with some options. It then handles results from this new dialog activity.
However, if the service notes that the activity is NOT open I want it to launch the dialog anyway. I know that this isn't good practise but because of the importance of this emergency I don't want to rely on Notifications (which are already in use if the activity is closed to let the user know that the app is still listening for emergencies).
What currently happens is that the below code is executed in the service and the dialog launches. However, the 'main' activity (the only other activity in the app) also opens behind the dialog. What I really want to happen is that either...
1) The service launches the main activity which then opens the dialog so that I can easily capture the results.
2) The service launches only the dialog activity and I use a broadcast receiver to capture results from this activity.
1 would use the mechanics that already exist for capturing results from an activity. However I don't like the idea of chaining the activities together in this way.
2 means I can ignore the main activity all together (because I don't really need it in this instance) but seems more of a get around.
What I am really asking is two things. What is best practise given my circumstances and how do i achieve number 2? Here is the launch code in my service. Notification in this code is referring to the dialog activity that will open.
if (MainActivity.isActivityInUI) {
//Dealt with by activity
sendMessageAlert(message);
} else {
//Launch dialog directly from service
Intent notification = new Intent(this,
EmergencyNotificationActivity.class);
Bundle args = new Bundle();
args.putString(MobileMessage.EXTRA_LOCATION_NAME,
message.locationName);
args.putString(MobileMessage.EXTRA_ID,
String.valueOf(message.id));
args.putDouble(MobileMessage.EXTRA_LATITUDE,
Double.valueOf(message.latitude));
args.putDouble(MobileMessage.EXTRA_LONGITUDE,
Double.valueOf(message.longitude));
//and the flag to let the notification know this is from a service...
args.putBoolean(EXTRA_FROM_SERVICE, true);
notification.putExtras(args);
//add flag because this is being called from outside of an activity
notification.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |ActivityInfo.LAUNCH_SINGLE_INSTANCE);
startActivity(notification);
I think instead of trying to show a dialog without visibly showing an Activity, you should consider launching an Activity that is themed like a Dialog. Just apply the following theme: http://developer.android.com/reference/android/R.style.html#Theme_Dialog (or similar themes) to your EmergencyNotificationActivity. You probably would have to tweak your class to behave like a dialog instead of launching one (which I am assuming is what you're doing currently).
This method would also allow you to not have to check if an Activity already exists.
I am new to Android . As per the Android Developers Doc making an activity launchmode singleTop it will keep that activity intact . But its not working for me .I have an Activity where i have a countdown timer , what i want is when i leave that Activity on back press and return to that Activity that countdown timer should still be running . How to do it ? Please Help
I believe you misunderstood a bit.
Launching an activity in singleTop does not mean that the activity is "intact", it means that if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent - a new instance won't be created. (This is opposite to launching activities in standard mode, which every time there's a new intent a new instance of the class is created to respond to that intent.)
As others suggested, you could bind to a Service and update the countdown time from there.
"Creating a Service will always keep that timer running , instead i want it to keep running only when application is alive", that means you want to continue the timer from where the user left.
Store the timer value in SharedPreferences onStop() and retrieve the same onRestart() and then finally continue updating
I am revoking a timer in android using intent now if i want to put that timer in a background when user clicks on directly back button then how do i do that? and then when i see background apps running it should be working and can be brought to front.
The problem is that if you define the counter in your Activity, the counter is bound to the Activity life-cycle. In order to workaround this, you can create your counter in a Service and just visualize the value in Activity.
There are also several techniques which you can use when defining the counter inside the Activity. Using SystemClock to track the time in your Activity in onStart() and onStop() for example, you can manually calculate the difference between those two values and adjust accordingly your counter.
I want to develop a program which reminds word (english-turkish). The things what I have to do below and please correct me if I'm wrong or bad way I'm using.
Create and Main.java class as an activity which includes a textview to show english word
Create another activity which shows Preferences to setup interval time to remind new words
Write some code under the Save button click(inside the Prefs.java class) to save settings to SharedPreferences
Inflate menu inside Main activity to show Preferences Activity
Create a service with MyService name.
Get interval from SharedPreferences inside the OnCreate method of MyService.
Inside the OnStart method Run a Timer according to interval and continousley connect to web service to get a new word.
Periodically bring to front Main activity(don't want to create every time from the begining, just want to resume activity) and show new word.
When pressed New Word use the service's function to connect and retrieve new word and show in TextView in Main activity
When pressed Ok set Activity to Pause mode and show Home Screen
I have some difficulties to Resume Main activity and passing new word.
Do you know a way to bring front Main activity periodically while it is in resume state?
Try to make an intent in the onResume() call with this flag: FLAG_ACTIVITY_REORDER_TO_FRONT which causes the launched activity to be brought to the front..
For more information click here.
There is no difference between starting activity and bringing them to foreground - read about activity lifecycle.
Service is ineffective if you want to use it as simple timer. Much better approach is to use AlarmManager and scheduling next activity start. Here you have an example.
Then just override Activity.onStart() method, to fill all fields you want.