I am having two page, in one page am having chronometer, after I left and come back the page the chronometer has stopped, any one can tell me how to redirect the page
My code:
Chronometer chrono=(Chronometer)findViewById(R.id.chronometer);
chrono.setBase(SystemClock.elapsedRealtime());
chrono.start();
thank you
I have no idea what you mean by "redirect the page", but it sounds as if you should read up on Android application fundamentals. Android activities do not execute when not in foreground. If you want to reset the clock once the activity is again in the foreground, you should probably override Activity.onResume().
Maybe I'm not understanding what you want to do, but it sound like you'd like to start a stop watch of some sort, go to another activity, then come back and check the amount of time passed. If that's your goal, you might be better off storing the start time in a variable, then when the activity is reactivated get the current time, compare to the start time, and compute the time passed.
Related
I have activity A with recipes that opens activity B for more options for a recipe.
In activity B I'm doing an action (like adding to favorites) that finishes activity B.
When doing that action, I'm triggering a Talkback announcement so that the user knows the action has been completed successfully.
However, the announcement gets interrupted halfway through because activity B is finishing and Talkback starts announcing activity A instead.
How can I make sure that Talkback announcements are not interrupted? Is there any way to change the priority in the API (similar to live regions?)
I also tried adding a Toast, but the toast announcement gets interrupted as well...
Any suggestions?
Thanks!
I have run into issues like these as well. I did not find a way to stop interruptions, but generally use the following approaches to get around the issues:
Keeping announcements short whenever possible. "Item With A Long Name has been saved to your favorites list" is too long, at that point user has a lot of context and has an expectation of what is going to happen, so short announcement is usually fine. Something like "Favorited"/"Saved" is to the point and takes way less time to be announced! This is important with translations as well, some languages are so much longer than others.
Used carefully and only very infrequently: Adding a delay. Send announcement, set a timer for 500ms or whatever it needs to be, accounting for translations lengths too, finish activity after that. This is something that ideally will not need to be used a lot: only for very important announcements which shouldn't be missed! Having delays could badly affect user experience, so for me this is the last strategy.
So, for my Android course this semester we're making an app of our own choosing, and due to a lack of ideas I decided to go with a text-adventure styled game. Nothing too complicated, I know. I'm making sure to be creative and incorporate a lot of different functions of the phone, such as the accelerometer and the camera. But I digress.
I've started planning out how the app should work, and how I should go about coding it, but I've come upon an obstacle fairly early that I need to find a solution for. I'm planning on creating it so that the player is sent from activity to activity, which I think is the best way to go about it, unless I do an endlessly scrolling activity that fills out as the player progresses. And thus, I need a way to make it so that when the player closes the app completely it will "continue" on the last activity before shutdown, so the progress is saved in a way.
I'm not sure if this is possible to do, and if so, are there any other ways I can achieve the same sort of result?
There is no way that you can change your starting activity programmatically. You can save the activity that you want to start with in a file and redirect to this activity from your main activity every time your application starts (on the onCreate() method of your main activity).
As it says here.
As someone has suggested, save the current state (ie: what Activity is currently shown) in SharedPreferences. When the user launches your app, the root Activity (the one with ACTION=MAIN and CATEGORY=LAUNCHER) will be started. In onCreate() of the root Activity, read the saved state from the SharedPreferences and launch that Activity immediately.
if i want to run a timer for a long time in android and show the user, when they go to a particular activity, for example, the duration since the timer started as a live number - how should I implement this? By live, i mean the time shown changes in real time as one would expect a timer to do. There will only need to be one instance of this timer, it'll pretty much be like android's stopwatch but implemented to function within a custom app.
Would I require wakelock?
should I create a service?
or should I just use a simple java timer?
any help/advice much appreciated.
thank you.
What do you mean 'long timer' 1 minut, day?
You have to realise that android can kill you activity any time, so the activity is not the right place to do it.
You can run you operation in Service - his life is longer then activity's, but you probably want to check time even if user returns in activity after week or reboot device.
If you tell more info about what you want it'll be easier to solve you problem.
show the user, when they go to a particular activity
As I see you problem the solution should be like this:
First enter to the activity - create timer, start it,
On activity stop save current value of the timer and system time,
On recreate activity read saved value and start new timer with value of init_value + (current_time - saved_time).
In this case you can be sure that timer is persistant even if user left your activity and even restart device.
And also battery life will be much more longer :)
Inactivity is a very important EVENT. For many apps if the user does not interact with it for a certain number of seconds its time to reset the app and go back to main activity logout, or conserve power. So I would really like to get some feedback on the best way to detect this. In fact I think everyone will benefit from a good solution to this.
So my question is twofold:
1) Is there a better way to detect user inactivity than using a combination of
activity.onUserInteraction() to reset a CountDownTimer?
Note: One reported downside to this approach is that softkeypad interaction might not be
caught by this approach.
Note: Another reported downside is the CountDownTimer is off main thread and might not update
correctly. I am not sure how big an issue this is?
Note: CountDownTimer appears to have cancellation issues as well:
how to stop/cancel android CountDownTimer
2) Lets say that onUserInteraction()/CountDownTimer is the best/only solution to this problem
there are still some questions:
a) should each activity launch its own countdown timer?
b) Should a single countdown timer be restarted in the onCreate method of each activity?
c) lets say I want to dim the screen or goto main activity when the countdown expires where
should the timeout handler be located? In each activity? In a service?
Thanks
Just stumbled upon this question as I've answered something similar just now.
Personally, I'd opt for option 2 that you have suggested, and put a timer into a singleton so its available across all activities. Theres no need for a separate countdown timer unless you have a specific requirement to react different under different features of your application.
Why would you want to reset the timer in the onCreate? You should do that each time the user interacts with the application, such as in the activity.onUserInteraction() method.
To quote from my previous answer:
You'll need to invest a little thought into exactly what your
requirements are here, but from what I can tell, you want to keep
track of the user interactions and if a time limit expires since the
last interaction, perform some action, in your case logging them out
of your application.
Firstly, you'll need some place that you can track when the last
interaction occured, since you'll want this to be application wide you
could use a singleton to hold this, or override the Application class,
either way should do.
Next, you'll need to start tracking user interactions. From your
activities, you can override the onUserInteraction method, this gets
invoked anytime the user interacts with the application such as key
event. Each time you hit this method, update your singleton and let it
know something has happened, with a timestamp.
Finally, you'll need some kind of looping check to constantly check if
anything has happened recently. Theres various was of doing this, you
could have a continuous loop that compares current timestamp to the
last recorded event, a bit of draft code :
while(true)
{
if (timeLastEventRecorded < (now - 15))
{
//nothing has happened in 15 minutes, so take corrective action
}
}
Presumably you'll already have some code in your application that
takes care of logouts, such as when the user clicks "logout", you
should just be able to invoke that in the sample above.
I am working on an Android app that deals with sensitive user information. One of the requirements is that the user is required to log back into the application whenever they leave it and come back. This is easily dealt with for the case when the user presses the Home button and then relaunches the app (android:clearTaskOnLaunch attribute on the Activity in AndroidManifest.xml). However, we need to do the same thing when the user long presses the Home button, switches to another application, then comes back.
I have researched this every way that I can think of and have not found a workable solution. Is this even possible with Android?
When answering, please keep in mind that this is a business requirement which I have no control over.
Well, I had the same problem yesterday. This is what I did and it works fine:
Added android:launchMode="singleTask" to the main activity in the AndroidManifest.xml
Called my boss and say: ey, this is going to take a long while... hold on!
Went and drank beer all night.
Just to clarify, my main activity only has a button that says login and launches the login page.
What have you tried? You can always clear whatever session you are saving in the proper Activity lifecycle method.
If I understand you correctly that you want to require an authorisation every time someone backs into the app, whether afresh or coming back to it, then you can override the onRestart() activity lifecycle event on the activity (or activities). So in onRestart() you can redirect the user to the login screen (you may also wish to consider onResume() depending on your requirements)
The lifecycle chart on this page will make this clearer:
http://developer.android.com/reference/android/app/Activity.html
Would it be possible to make it a time based thing, rather than strictly left the app and returned?
You could have a separate service that keeps track of when the last time the user accessed the application was.
I.e, in each onPause the Activity tells the service that an Activity was paused. The service records the time of that.
In each onResume, the Activity informs the Service that it wishes to resume. If some amount of time has passed since the last onPause, then the Service indicates that a login is required.
I think this would make a nicer user experience than just every time they leave the app. That could be very frustrating, to take 30 seconds to read a text, and then have to sign in again.
I suppose if you tweak it to have the timeout be very short, it has very similar behavior to what you requested anyways, but with the option of making it less draconion.
I think the easiest way to implement this, would be to add a field to your main activity like private boolean isLocked = true;.
To lock the app when another one is shown, set isLocked = true in the onPause() method. To make sure, that your don't lock your app, when returning from your own activities, start them via startActivityForResult() and unlock it in onActivityResult.
You can now check in onResume() wether your app is locked and redirect the user to your login screen.