android and multiple running of the same application - android

I write application on android which will be runnig all the time on background. There will be only one starting view on first run. I want to user run instance of my app only once, and cant run any other instance at the time. When he try to run this app when one instance of this app is running already he sould see some warning notification. My problem is I dont know how to prevent user from multiple start of my app. Is this possible? If it is possible, how can I do it? Thanks for any help.

For background processing I would recommend to consider Services. Services are created to deal with longterm background tasks. I think foreground service(like Skype) may be interesting for you.
As Phil suggested you can you launch mode to control your activity behaviour. Consider using launchMode = "singleTop"

You need to pick a launchmode that will best fit your needs (probably singleInstance or singleTask). As for popping a notification, you can handle that in onCreate or onResume, however it doesn't have anything to do directly with how many instances are running.

Related

how to leave my acvitity to continue run when the apps is not on front ?

I wrote some application that write to file the GPS location every 30 seconds.
When the application is not on focus i want to continue to write to the file.
How to do it ?
That's not possible due to the lifecycle of an Activity. As soon as you lose focus or hide it it will go into the corresponding state of onPause, onStop or even onDestroy if its not needed anymore.
What you have to do in order to fix it to start a Service. There are different kind of services and in your case an unbounded background service would do the job.
You possibly want to start it in the onStop() and stop it at onResume methods
See here for more android documentation on services
https://developer.android.com/guide/components/services.html
To get things to continu working after the app is send to the background, you would need to use a Service instead of a Activity.
Check out this link, the Android Developer website about services:
https://developer.android.com/guide/components/services.html
Also read this, about the fundamentals of Android apps:
https://developer.android.com/guide/components/fundamentals.html

Is it possible to start an Android app without putting it to the foreground?

Is it possible to start an app in the background? For example, if a push notification is received, then the app is started in the background,without invoking the UI/screen?
Because at the moment i have to invoke the app to the foreground and then put it back to the background which causes flickers.
You can use Android Services, they can operate in background and have their own lifecycle which can be managed. If you want to process in worker thread, use Intent Service. Developer Docs which I have given link of provide sufficient information about implementation.
Is it possible to start an app in the background
Not really, but there is no obligation to show any UI once your app is started so that'd would look like you started in background. In fact it'd be perfectly safe to have all the setup done without any UI. Calling setContentView() in your Activity's onCreate() is not mandatory as is concluding your onCreate() with ordinary finish(). That way you can i.e. start Service w/o showing any UI. There's even theme you can use with activity you do not want to display:
android:theme = "#android:style/Theme.NoDisplay"
For example, if a push notification is received, then the app is started in the background,
This is different story. Stop using Firebase's "automated" handling and process all pushes yourself. See How to handle notification when app in background in Firebase

Where is the best place to start a long running, application-wide, background task

I have a long running background task that I would like to start when the app launches and shutdown when the application shuts down. I'm already quite aware of the activity life cycle and what gets called when an activity gets created and destroyed.
I'm coming from an iOS background, and over there we have some calls that are made during application startup and shutdown. Is there something similar in the android world? I've searched a lot and all I'm finding are answers relating to an activity, not the entire application.
(Android is relatively new to me, so I may just not know the correct terminology to search for.)
EDIT:
I'll try an be a bit more specific. I have a background task that needs to be continuously running while the user is using the application. It will be streaming data from a server continuously while the application is active. It does not need to run when the application is in the background. It doesn't seem to make sense to me to tie the startup / shutdown of this background process to any one single activity since it may not be the same one activity that starts up when the application becomes active.
I am (possibly mistakenly) assuming that the OS takes care of starting / stopping background threads when the application resumes and pauses. If that is, in fact, the case, then all I really need to do is spin up the background task when the application first launches, i.e. when it is loaded into memory and becomes active for the first time that session.
It doesn't seem to make sense to me to tie the startup / shutdown of this background task to any one single activity since it may not be the same one activity that starts up when the application becomes active.
That's reasonable. It is somewhat difficult to implement, though.
I am (possibly mistakenly) assuming that the OS takes care of starting / stopping background threads when the application resumes and pauses.
You have it exactly backwards. Android pays not one whit of attention to any threads that you fork yourself, directly or via thin wrappers like AsyncTask.
In addition to that point of confusion, you appear to be equating "user switching to another app" with "app shutdown". Those may be the same thing in single-tasking operating systems. They are not the same thing in Windows, OS X, Linux, Android, etc.
So, what you seem to be seeking is having a background thread running doing this streaming work while your UI is in the foreground, and then stop when your UI is in the background. The problem is that there really isn't a straightforward way of accomplishing that in Android.
One close approximation would be to create and register a custom Application class, where you override onTrimMemory(), and stop your background work when you get to TRIM_MEMORY_UI_HIDDEN, TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_MODERATE, or TRIM_MEMORY_COMPLETE -- whichever of those that you encounter first. If, when one of those arrives, you determine that your streaming thread is still outstanding, shut it down.
In terms of startup, you could use onCreate() on that same Application singleton. The problem is that this will be called on any process creation, which may include scenarios in which you do not have UI (e.g., you are responding to some system broadcast, like ACTION_BOOT_COMPLETED), or possibly your process is going to parts of your UI that do not depend on the streaming. If you have none of those scenarios, then onCreate() in Application would be fine. Otherwise, kick off the streaming in onCreate() of whatever activities need it.
While normally we manage long-running threads with a Service, that is for cases where we explicitly want the thread to continue after our UI is in the background. Since you do not want that, you could skip the service.
It depends on what you want to do exactly. When you're just interested in the app starting for the first time you could #Override onCreate().
Or maybe you want to use onResume() as this will get called whenever a user brings the app to the foreground.
But this really depends on what exactly your background task is doing and what you want to do with it, to get an exact answer you need to provide more details.
Here is an overview for the actiity life cycle that should help you:
You can extend the default Application class and implement it's onCreate() method to detect when the app is launched. There is no corresponding method for when the app gets closed though.
Do not forget to specify it in the Manifest file.
In Android the application isn't shut down unless the system runs low on memory. You won't get a warning about that, it will just call your Service's onDestroy lifecycle method. If you want to do it when the Activity is visible on screen, use onStart and onStop. If you want to do it when the Activity is resident in memory, use onCreate and onDestroy.

Android App Exits when Going into Background

Ever since I added a new Class to my Android app (specifically, a sqlite helper class) may app wants to relaunch after I press the home button. Before adding the class, the app would multitask as expected.
I am stumped. It seems onDestroy is called every time the app goes into the background.
Any tips or thoughts as to why this would happen?
This is by design. Please refer to Android activity lifecycle for more info when/how your activity could be destroyed. Basically, as soon as your app goes into background, your activity can be killed at any moment.
If you want to continue execution, you need to create a Service that represents a long-running components in Android architecture.
It turned out I had android:finishOnCloseSystemDialogs="true" in the manifest file.

Monitoring of Activities visibility

Is it possible to determine the moment of switching of certain Activity from foreground to background and vice versa?
This activity should run in the separate process.
I need to write the application that collects some statistics from using of big set of applications (app names read from configuration file). My application works as Service and should remember moments of switching of activities between foreground and background.
Set of applications is sufficiently big and most part of these applications will never work on certain phone.
I don't know a lot about that but I think you could try to do some digging in that direction :
onFocusChangeListener
by applying it to the whole activity (refered as "this" in your onCreate for instance)
What about tracking intents? If you play around on a phone or emulator while looking at LogCat, you'll see that every time an activity is started or accessed there's a log message about the intent that was broadcast. I'm not sure how your program could intercept and process these, but it's another place to start looking.

Categories

Resources