Background app best practices - java

Is Service and Notification the correct way to implement a background app with the following behavior?
1- User open the app, make some configurations and touch in a "run" button;
2- The main activity must be closed and a background service will be started;
3- A fixed notification will be displayed with some buttons ("stop" to finish the service and "Reconfigure");
thank you

I did an app that at short intervals (1 to 10 sec) acquires constantly data from a remote TCP server, even on the background also with the screen off.
In the Main Activity onStop I acquire a Partial WakeLock and I start the Service with STARTFOREGROUND_ACTION intent, the Service calls startForeground and shows the Notification.
On Activity onResume the WakeLock is released, the Service stops with STOPFOREGROUND_ACTION intent and the Service itself calls stopForeground & stopSelf.
I don't have buttons in the notification, if user touches the notification the Activity is shown.
It works very well I tested it with hours of continuous operation.

Related

Android: turn off display without destroying or pausing activity

i have a mediaplayer in an activity, i want to implement a feature in which when a user clicks a button, the screen display goes off, but the video continues to play in that activity.
Acording to the life cycle of the Android activity, when an activity becomes background, onPause is called, however, from now on, the system can kill the activity by its will, ie. the system requires more memory.
It'S the system that decides to kill the activity or not, not the developers. In general, It'S because more memory are needed.
Understand the Lifecycle Callbacks
You need to implement a Service. Start at this link on how to do it: https://developer.android.com/guide/topics/media/mediaplayer.html#mpandservices
activity should be paused
you can override the procedure onPause() and onResume() to check the event
when pause, you pause the media, when resume you resume the media
the media is playing the other thread and draw the activity. when the activity is pause, but the media thread is working.

Android Wear: Resume previous activity state from notification

I have an Android Wear app that contains an activity with a timer and a view rendered accordingly. When a user swipes right on the activity to get back to the Context Stream a notification appears showing that the App is still counting.
However the user is unable to resume the timer activity from the notification intent because a brand new instance of this activity is launched. I know there is a FLAG_ACTIVITY_REORDER_TO_FRONT flag I can add to the intent but because activity is destroyed when the user swipes right on it, this does not work.
How can I properly resume the activity from the notification. I guess I somehow need to prevent it being destroyed on swiping back?
Thanks in advance!
Once you leave the app, your activities will be destroyed, so you will have to save the time you started the timer somewhere (Shared Preferences is a good option), and then read this in when your app is restarted.

How to monitor if my App is paused or resumed?

I have an Android app where I want to track when the app is paused or resumed.
paused: User pressed the home button and the app is still running in the background.
resumed: app runs in background and user opens the app.
How can I being notified when my app was paused/resumed?
paused: User pressed the home button and the app is still running in the background.
I am going to guess that the initial state is that one of your activities was in the foreground at the time the HOME button was pressed.
On the whole, there is no notion in Android of an "app" being in the foreground or the background, though we sometimes use that phrasing as shorthand for other scenarios.
Whatever activity was in the foreground will be called with onPause() and onStop() when the user presses HOME, but those events are also called in many other scenarios (e.g., user presses BACK). onUserLeaveHint() will be called when the user presses HOME but not BACK, but onUserLeaveHint() is not called in other scenarios (e.g., incoming call screen takes over the foreground). Whether onUserLeaveHint() will meet your requirements, I cannot say.
resumed: app runs in background and user opens the app.
onStart() and onResume(), at minimum, will be called on your activity that takes over the foreground. Those will be called at other times too, such as when the activity is coming onto the screen for the first time. There is also onRestart(), which will be called only if the activity is being started after having been stopped (i.e., after a prior onStop() call), which will weed out the newly-created-activity scenario. However, onRestart() will be called in other scenarios as well, such as part of a configuration change (e.g., screen rotation).
In general, what you are seeking is not really part of the Android architecture. You may need to approach your problem in some other way.
In you Android activity you can override the onPause and onResume methods.
See the documentation on Lifecycle Callbacks for a list of other lifecycle callbacks that you can implement.

How to cancel timer when home button is pressed? [duplicate]

This question already has answers here:
Checking if an Android application is running in the background
(35 answers)
Closed 8 years ago.
I have a countdown timer in my game and I have to cancel it once the home button is pressed. I tried with this:
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
timer.cancel();
}
But this does not work for me, cause I have some popup activities and some other stuff because of which I lose the focus of my main activity and that makes the timer to stop, and that's not good for me. Is there a way to stop my timer ONLY if the home button is pressed?
try this
#Override
public void onStop() {
super.onStop();
timer.cancel();
}
This is a common type of question, with no trivial, built-in solution. But easy enough to write your own.
The basic solution relies on monitoring the onPause() and onResume() events of all your activities. This would normally be done by using a base Activity class that implements the below behaviour, and all your activities extend it.
I normally use my Application class to handle the logic of knowing when it is no longer in the foreground. So in the application class, I have methods that get called every time my activity pauses, or resumes.
when paused:
I start a short timer, say 300 milliseconds long
when resumed:
I stop that timer, if it is running
So:
if you launch your activity B, your Application class will get a pause signal, followed right away by a resume signal. Your timer will not expire.
if your application is sent to the background, you will get a pause signal, and no resume signal. Your timer WILL expire.
When the timer expires, you know that your application is in the background, and you take action. In your case, that means cancelling the game timer. Other people will log out of a server. In my case, I turn NIGHT MODE off.
You can base the code on my answer to a similar question, which shows how each activity would alert the application of its status:
How to know in BroadcastReceiver if App is running on foreground?
In that question you can see a boolean flag is being set. In your case, you would start or stop the timer.
For more details, check out these related links:
Android auto-logout when app goes to background
Run code when Android app is closed/sent to background
Checking if an Android application is running in the background
Maybe you should stop your timer when the activity's onPause or onStop method is called.

Receive volume key press events when phone is in deep sleep

I'm trying to catch Volume Up/Down pressing events when phone is in deep sleep mode. I have read several articles and here what I have done.
In Activities onCreate method I set a WakeLock
PowerManager mgr = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
I have read that even if my screen is locked if I set this my application will respond to events. Also I have added permission in to Android Manifest.
<uses-permission android:name="android.permission.WAKE_LOCK" />
Then in the onCreate method I declare my Broadcast Receiver
VolReceiver volumeBroadcastReceiver = new VolReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("android.media.VOLUME_CHANGED_ACTION");
registerReceiver(volumeBroadcastReceiver, filter);
This all works pretty good then application is in foreground or background, but then I lock my phones screen by pressing on a power button application stop receiving broadcast events, I think that PowerManager must solve this issue but it doesn't. So please help me, provide some information.
I'm trying to catch Volume Up/Down pressing events when phone is in deep sleep mode.
By definition, that is impossible.
In Activities onCreate method I set a WakeLock
Then you are not in sleep mode.
then I lock my phones screen by pressing on a power button application stop receiving broadcast events
There are a few possibilities here. One is that your process was terminated, as it is no longer needed. Once you no longer have any foreground activities, your process is eligible to be terminated to free up memory for other apps, and that can happen at any point. Another possibility is that Android simply does not send that broadcast when the screen is off.
I did pretty much the same thing, but i achieved it by changing the source codes. i have explained that below.
whenever your phone goes to sleep , your MediaPlaybackService.java will not listen to keyEvents, but MediaButtonIntentReceiver.java will, so receive the intent here of volume up and down, and broadcast an intent and receive it in MediaPlaybackService.java, but keep one thing in mind you can't change the UI from here , so you can broadcast another intent from the service and make your MediaPlaybackActivity.java receive it , this will change the UI as soon as your screen wakes up.
FYI: when the screen is off, the PhoneWindowManager.java queues all the continuous intents and as soon as you release the button it will apply all the intents at once.

Categories