I'm developing an Android app with several types of alarms and triggers.
One of these alarms trigger if you stop moving (GPS tracking) while it's active.
Now, when the app runs in the background when this triggers the client doesn't update when you switch back in, and the only notification received is a push-notification from the backend service. If I enter through the notification, the client loads the alarm correctly.
The code base is quite extensive, and due to time and resources it would be best to avoid huge refactoring tasks.
Is there an easy way to make the app go from Activity A to Activity B when it's running in the background?
You cannot change the current Activity in the background without bringing the app to the foreground (using startActivity()). However, you can surely tell the app that when it is brought to the foreground it should start a certain Activity or rearrange the back stack or whatever.
Post some of the code and maybe we can help more.
Related
My application currently has two activities: a splash activity which displays the app logo and loads the user’s data from a remote database, and then the main activity that houses all my fragments. I’ve reached a bit of an impasse right now with notifications. I’m using FCM to deliver the notifications and have that working fine, but don’t really know what to do with the pending intents of the notifications. If the app is closed, I don’t want it to launch the main activity since I load the data from the DB in the splash activity. Thus the main activity would have missing data (it currently crashes due to null pointer exceptions everywhere). But if the app is still running, I don’t want the splash screen to be launched again. Is there some type of way to have a conditional intent baked into the notification?
Alternatively, is this just bad practice to do what I’m doing with launching the activities from the notifications? I’m by no means an android expert so there might indeed be a better way. I would prefer to keep my current activity flow however where the splash screen is responsible for the initial loading of data. I was hoping there was some way to just broadcast a message on the click of the notification and if any activities have an active listener then it would just consume that data.
The solution I have come up with is to have a single activity whose sole purpose is to process notifications and then pass them to the appropriate activity either through a new intent (if app is terminated) or through a broadcast (if app is resumed). Not sure if this is bad practice (would appreciate anyone telling me if this violates any type of software principle).
It's working like a charm so far and is honestly really nice to have all that logic in one activity anyway - all the service has to do is pass in the data from the notification and the action and the activity does the rest.
I want to know if it is possible to show an imageView even if the app is not in foreground or is killed like ads are displayed after some time intervals even if the app is closed
If yes, how can we do that.
It is possible to run code even if your app is not in the foreground through using services like JobScheduler or WorkManager in Android.
This is how apps such as Alarm Apps can ring at a later time even if the app isn't opened.
However, keep in mind that it is DEFINITELY NOT OKAY for you to run ads OUTSIDE of your foreground App. To schedule for ads to appear when you're app isn't running is easily considered as behavior for malware and it's an easy ticket to getting your app killed off and potentially your Developer account closed for all eternity.
But if you want to show an Image at a later time even if your app isn't running, for legitimate purposes, then you can do so through scheduling jobs through the services I mentioned in my first line.
I repeat.
It is NOT OKAY to show ads after your app is placed in the background or after it is destroyed.
It is NOT OKAY to show ads at a later time if your app is not the foreground app.
I have an app that sends TCP messages. I need the messages to be sent as long as the app is alive. So, I granted access to battery optimization and added all the required wake_locks and everything seems to work fine even when screen is off.
The problem is that I noticed that every time that I leave my phone with the app in the background, when I come back after few hours(or less) no messages are being sent, and when I enter my app it loads as new instance and not like an app coming from background. What can I do to have my app not being killed by Android?
I guess that this is what happens
You need to use a foreground Service; a service started via startForeground().
The service does not "re-open" your activity; it is a component of your application that may be long-lived. You might prefer to think of it as an activity without any views.
Yes, as the others mention you have to use Service for background execution.
Additionally, also, keep in mind of the Background Execution Limits for Oreo and above.
You can also look at JobScheduler for managing asynchronous tasks efficiently.
Create a 1px*1px transparent view on the desktop, such as toast.(the Android os will level up your priority as a foreground process)
Suggest your user to add your app in the white list.(in some rom)
Always put your app`s notification in the notification bar.(also need a service)
create a guard process, when your app die, send a broadcast to guard and let guard restart your app. when guard die, send a broadcast to app and let app restart your guard.
Be careful of battery consumption and don`t trouble your users too much
I am developing an app which requires the gdk program to continue running as-is even after the device is removed from the head. Currently the program is paused when this happens seemingly by default, but I need the program to stay running because it is constantly uploading video. The desired result is that the program will continue to run and upload a video stream even if the glass is removed from the user's head.
What can I do to change this behavior?
thank you.
You probably want to perform the long running upload in a background service. Android's activity model makes it such that you can't really depend on the life of the activity to always be in the foreground and you need to use a service for tasks that shouldn't be paused.
Also if you have an activity you want to return to when you put Glass back on, ensure you specify android:immersive="true" within your declared <activity>. Without this, your activity could be completely destroyed when the screen turns off.
I wonder if it is possible to have notifications show up even if the app is turned off.
say for example I have an app and I set some kind of notification for 13.00 the next day and then turn the app off or restart my phone or something.
Is it possible to still get a notification in the notification bar with some text that starts the app or do the app need to run in the background to be able to use notifications?
if it is possible, what is the best way to do this? (get a notification at a certain time that starts the app) and if not, how do you recommend doing notifications?
The type of notifcations I want to do are reminder notifications.
thanks for all help!
A combination of AlarmManager and Service will help you to perform the task you are looking for, I had performed this thing.Check out the android tutorials for it.
What you're looking for is a background service. See this Android Services tutorial.
In the background service, your app should check periodically and when necessary, create and issue new notifications. See this Android Notifications tutorial.
The background service can be set to automatically start when the phone is restarted, and to re-issue any uncleared notification again.
Many apps such as Gmail already do this. Although you must be careful in resource usage or Android might kill your service.