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?
Related
I got a situation where i have two APPS for simplicity keep it as APP1 and APP2, i am passing an object remoteCallback as CALLBACK from APP1 to APP2 for future use.
where based on result APP2 will instantiate a method updateStatus(String msg) using a object remoteCallback, Everything is working fine but, when i close the APP1 and clears the memory, APP2 is unable to call the method i know what causes this problem i want to know is there any way to make the object(remoteCallback) live even the APP1 closed.
Thanks in advance.
I'd use an unbound Service here. Unbound services make that even if one of your apps close, if the conditions permit it, it will still be running in the background. If you used a bound Service, it would stop with your app if your app stopped.
This differs from an AsyncTask or a Thread, because even if they run in the background, there's a big change of being killed if Android needs more memory or is in lack of resources.
This introduce, however, a new responsibility for you: You'll have to make sure you use startService() or stopService() accordingly within your app and don't leave your Service running indefinitely.
This seems to be a good example about how it works, but you might find lots of documentation about unbound Services
There is no way we cant stop an app from destroy so instead of passing an callback to get details use BROADCAST RECEIVER.
This is the solution i adapted to this situation.
"I want to know is there any way to make the object(remoteCallback) live even the APP1 closed."
PendingIntent ?...
Its the same mechanism used on notifications.
You can start the background service and the store the object which you want to live object after the app is closing
you can declare the public static data member and you use the whole application
How to detect user activity/inactivity in android service/background application - from same or other applications?
I want to know that user stop touching screen and not busy with another activity to show notifiation popup. Not sure is there no other method than use AccessibilityService.
One of the ways is to know that from reading the Android logs
Use a boot receiver to setup the AlarmManager (and of course also check to start the polling from your main Activity too, for the case when your app is installed and the system is not booted) and have the AlarmManager send an Intent for another receiver.
I think they answered it in this question to do what you are looking for:
android: running a background task using AlarmManager
Define a interface in activity and service.
in oncreate of activity set reference to activity in service method.
Now you have activity reference for entire service lifetime.
another way to do this is, application class, same application class is shared by activity and service, so you can both of these from each other using application class.
If you want see from applications then use broadcastreceivers that is the only wau
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
I need an advice for my latest app. It will show the user the latest subtitles released, and it will give him a notification in case new subtitles of his favourite series have been released; what should I use to achieve this?
I was thinking to create and run a service which will include a
timer.scheduleAtFixedRate(new TimerTask() {...
but at the same time I really don't know how to make it interact with my app (if the app is opened I don't need any notification but I need to update the GUI).
I could use a thread but I'd like it to run it even after the main activity has been killed...
or I could use a AsyncTask so it would be easier to deal with the Application GUI.
Which solution should I use? I was thinking I should simply use a service (the first solution), but I'm not too sure about it, and furthermore I don't know if there is any way to make a service communicate with an activity periodically...
Thanks for your help.
A service communicating with an activity is called bound service, that's what you should use IMO.
However, you say that when the activity dies, the service should keep running. But if the service is bound to your activity and the activity is dies, the service dies too.
I suggest you to read here about it.
Check and see if you can bind a service to an activity, and when it dies, unbind and let the service continue to run independently.
If you can't, the activity could unbind itself, then start the service independently (with startService rather than bindService).
if you are showing notifications, why not use C2DM messages for communicating with the app. The only thing would be that there would be popups shown to the user even if your app is not running. No need to use threads/services.
Probably it's not a really complicated question, but first of all, I have no idea, what search query should I search for?!
At the beginning of my application I would like to start GPS and if my application will be enden GPS should be closed.
How can I check, if the whole Application (not an Activity) is finished?
Is it enough to use onDestroy-Method for Start-Activity, which will never closed with finish()?
Thank you very much and sorry for a beginner's question.
Mur
UPD
I saw the first answer and I'd like to say once.
I don't mean an ACTIVITY, I mean really the whole APPLICATION (in which many activities exist).
How to check if all application's activities were finished and only in that case stop the service?
Is there posibility for that?
UPD2:
I've tested my solution on a device:
"Is it enough to use onDestroy-Method for Start-Activity, which will never closed with finish()?"
Yes, it was enough.
Make it so that, each Activity binds (via bindService) with the Service...When all the activities have been terminated (unbinds implicitly), your Service will perish. Since the Service will remain alive as long as someone is binding with it.
In this particular case what you need to do is:
Create an Activity to show information to the user.
Create a Service that will run on background and will send the updates to the Activity
There are lot's of examples of what you are trying to do, but basically you can start the Service on the onStart() method of your Activity and ending the service on the onDestroy().