I made an app that makes streaming radio. How can I prevent that, the second time by clicking on the icon to open the app twice?
Here is one good suggestion:
*
How to launch activity only once when app is opened for first time?
What I've generally done is add a check for a specific shared
preference in the main Activity: if that shared preference is missing
then launch the single-run Activity, otherwise continue with the main
activity. When you launch the single run Activity create the shared
preference so it gets skipped next time
This solution is slightly different than the question you're asking, but should give you exactly the result you're looking for.
Another possibility might be to use android:launchMode = "singleInstance" in your androidmanifest.xml:
http://www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode#
IMHO...
To stream the music you should be using a Service so that you can keep it going even when your app is closed. At that point your Activity is only used to communicate with the Service. When you launch your app just check to see if the Service is running or not.
Typically this shouldn't happen, if your activity is already running the system will bring it it up if the user relaunches. If your activity is launched by another app/activity in a new task, or for some other reason an intent may be used to create a new instance of your already running activity, look into singleInstance or singleTask modes in the manifest:
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
As for streaming, you should use a Service (preferable a bound service) to contain your media player so that it can continue to play in the background, then the activity is just a client of the service that connects to it, and the service keeps a single instance. That has been described here:
Set Aac+player app as service in background
Related
In my app I have two activities, A and B. A downloads some data and starts activity B when the data is downloaded - this is done through a BroadcastReceiver whose onReceive() method starts B through an intent.
My problem is that, when I my app goes to background while the data is still downloading (e.g. I start my app, then quickly click the "home" button, or switch app with the square "switch" button), my app comes back to foreground once the data is downloaded... annoying.
So far I've tried adding flags and setting actions on the intent to avoid that, including intent.setAction(Intent.ACTION_SCREEN_ON), which might be close to the answer: it works nicely on a device running API 16 but not on mine which is running API 22.
Typically, one uses a service to carry out operations in the background. Services don't have a UI (for obvious reasons). By definition, an Activity is a foreground task - the behavior you're seeing is exactly what is supposed to happen.
I'd strongly suggest NOT trying to turn an activity into a service. You'd be working against the whole application life cycle when you could use a feature in Android designed for the very purpose you seek.
Here's a quick resource, I suggest you read more on services:
https://developer.android.com/training/best-background.html
The BroadcastReceiver for an Activity receives and processes Intent objects even when your app is in the background, but doesn't force your app to the foreground. If you want to notify the user about an event that happened in the background while your app was not visible, use a Notification. Never start an Activity in response to an incoming broadcast Intent.
In android how to make sure that the service that I have in the application will get called only through an activity that is within the app.
(In other words I want to limit the service to get played by only a certain activity & not even by other activities within that app)
I tried studying Intent-filters but got a bit confused.
Can someone please suggest, if possible with an example?
Thank You
Use Context.startService(Intent service) to start the service from your activity.
Despite the name it doesnt only start the service. If the service is running already, it just calls it.
From the service perspective, the service will then call its onStartCommand(...) method.
Only an activity within the same application can call/start a service this way.
If you set exported="false" in your <service .. /> element of AndroidManifest.xml, the service cannot be called by activities outside your own app.
I know of no way to limit access to any particular activity within the app, but this seems a less pressing concern. Supposedly you can trust your own code?
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.
While application will be started, I want to launch one activity if some condition is satisfied else want to launch another activity. How to do this?
Start the one, and in onCreate() check the condition. If it is not satisfied start your another activity.
you must do it handling all conditions in an initial activity, which will check your rules and start the other activitys.
I don't know much about your particular use case, but if your application has to launch different activities depending on the task it is supposed to do, you should consider having appropriate intent-filters for each, and sending an Intent that's in accordance to the intended effect. That will make the correct activity be launched depending on the Intent, which is one of the great features of Android.
But of course, if you can only decide which activity is best once you launch your app, this approach doesn't work, and you have to launch an initial activity to decide which one to call next.
If it's highly likely that one activity will be launched, you might consider launching it as the first activity and then testing whether you need to launch another instead. That's probably cleaner than having a separate activity just to make the decision about which other activity to launch.
Bruno Oliveira, Developer Programs Engineer, Google
I have an android app. I added an intent filter to one of my activities, so that if a url is clicked in the browser app, my activity can be launched. Looks like this:
<data android:host="www.mysite.com" android:scheme="http"></data>
this works well. The problem is that every time my activity is launched from the browser, a new instance of the activity is created, inside the browser app's task, instead of recycling any existing instance that may be in the system already.
This is a problem for me because this activity uses a singleton. If I allow more than one instance of this activity to exist, I can get into some weird situations where the two instances are in conflict when they try to share the singleton. I checked and can see that although the activity instances may be in separate tasks, they do share the same singleton instance.
An ideal solution for me would be if I could somehow bring a pre-existing instance of my application to the foreground, and launch or resume the target activity within whatever pre-existing instance of my app happens to be running.
So I tried this:
Register above filter to point to dummy activity which is just a catcher.
Dummy activity creates a broadcast intent, and tries to broadcast 'create me' message to system.
Real target activity is set to listen for this broadcast message. Hopefully if there is already an instance of the activity in the system, it will come to the foreground. If no instance yet, that's ok, allow creation in the browser task.
Not sure if this makes sense. My basic goal is to limit the activity to one instance in the system. The app is just social media app which has a login state that needs to be preserved. The singleton mentioned above preserves that login state, so I want to have only one around in the system, instead of allowing multiple login instances running around which would be a headache for the user.
Thanks
You can set the activity launch mode to singleTop to achieve this.
android:launchMode
An instruction on how the activity should be launched. There are four modes that work in conjunction with activity flags (FLAG_ACTIVITY_* constants) in Intent objects to determine what should happen when the activity is called upon to handle an intent. They are:
"standard"
"singleTop"
"singleTask"
"singleInstance"
The default mode is "standard".
https://developer.android.com/guide/topics/manifest/activity-element.html#lmode