[Q] Android Service - Android Software Development

An Activity that started a service finished. That means the process the Activity run in has finished. So all of threads the process created would stop. But why the thread serving the Service that the Activity started through startService() method is alive?

I'm not sure what you're asking. A service will continue to run until it ends itself, even if the activity that launched it ends.

Gene Poole said:
I'm not sure what you're asking. A service will continue to run until it ends itself, even if the activity that launched it ends.
Click to expand...
Click to collapse
I'am sorry, if I'am extremely obvious. SDK says a service is not a process and is not a thread, but it runs in the same process as the application it is part of. What is it? Why can it keep running after the process finished? I'am a little confused.

"Process" with respect to the JVM is just an execution context within the virtual machine. In the typical case as I understand it, an app, activity, service, etc. can exist in the process space and can interact with each other, but not necessarily depend on each other.

jiyeyuran said:
An Activity that started a service finished. That means the process the Activity run in has finished.
Click to expand...
Click to collapse
No. You are confused and you think "Activity" = "Application".
jiyeyuran said:
I'am sorry, if I'am extremely obvious. SDK says a service is not a process and is not a thread, but it runs in the same process as the application it is part of. What is it? Why can it keep running after the process finished? I'am a little confused.
Click to expand...
Click to collapse
It cannot continue after the process finished.
This simply means: the process has not finished! :facepalm:
Read here.
Application = Activities + Services + Receivers + Providers.
When the first one is called, the Application process is created.
When the last one has finished, the Application process is no longer.
So if you start an Activity, a process is created.
Then you call a Service from the Activity, it gets added to the same process (unless you specify a different process, check the docs link I gave you).
When your Activity finishes and the Service is still alive, the process will live on.

Thank you very much. Your explain is very clear. Without specially indicating, a service from the Activity and the Activity will run in the same thread. But if startService and bindService work at the same service, you need to very take careful of using them. For example, if you start a service with startService call and bind the service twice, then you unbind the service, an exception will be waiting for you.
I have spent my weekend in finding out the service mechanism. Hope you can understand my poor English.

jiyeyuran said:
Thank you very much. Your explain is very clear. Without specially indicating, a service from the Activity and the Activity will run in the same thread. But if startService and bindService work at the same service, you need to very take careful of using them. For example, if you start a service with startService call and bind the service twice, then you unbind the service, an exception will be waiting for you.
I have spent my weekend in finding out the service mechanism. Hope you can understand my poor English.
Click to expand...
Click to collapse
I don't understand if you are asking a question or not.
Read the link I gave you, it has a brief and simple description to understand when you need to use an Activity or a Service (or a Broadcast Receiver or Content Provider, yeah).
What are you trying to accomplish, anyway?

Our teacher ask us to develop a SmartHome Manage software on the android platform. The application can turn on and turn off some appliances in home like frigo, tv, window, etc. It can also receive the telephone messages, so we may need to download voice files from the service runing at a computer. Obviously, android service is a good choice to spawn a thread for downloading voice files. As cutomers can take other service operations during the period of downloading voice file, the service may be binded many times before it is unbinded. I wonder if it will cause problems when a service is binded repeatedly before unbinded? Which aspects we need to take care of when we use android service?

Android SDK says when binding a servcie, onCreate() (if service don't start) and onBind() will execute. When you bind again, the onRebind() will excute if the return value of onUnbind() is true. But the problem is that when I bind secondly, the first unbind haven't returned yet. As a result, the onRebind() method cann't be executed. Does it cause problems?

Related

Communication between Service & Activity

Hello
I want to create an Android App. In the last days i read a lot of Android API Documentations, Tutorials and "how do do's". Now I'm really confused, because on one hand, it's nice to have so many possibility, but on the other it confuses me, whats the best way to do it. So I played a little with Activity life-cycle.
Now I'm going to start to build my first 'real' App.
This App should download initial data from a Webservice, process these data with a database, download more data related to results from a database, save these data again to database, and make it accessible in the Acitvity.
So please correct me if I'm wrong from this point on:
I assumend that a Android Service is the best way, to process these Data in a background-worker-thread.
So far so good. So I read about the Android Handler which seems to be a pipeline, so Threads can work on the Handler message queue. I can Access the Service from within my MainActivity over the ServiceConnection.onServiceConnetcted where I recieve a simple Binder which gives me access to my Service. From there i can put Messages in the Message queue from the HandlerThread. But how I can tell my Acitivity from within the Service, that it has finished Processing and send the Data to it?
I read that I don't have to use AIDL since the Service is running in the same Process. But How can I do it then? I tried to call onBind() in the Service, hoping OnServiceConnected will be triggered in the Acitvity which initially Binds these Services, but it doesn't seem to work. I also tried to "hack" the Funktionality, by spending my Service a singleton member "MyMainActivity" with corresponing static setMainActivity(MyMainActivity activity), calling it in the ServiceConnector onServiceConnected, with the same result: Runtime Error
I even don't think I understand the functionality behind these HandlerThread.
will it loop infinitly waiting for HandlerMessages while draining the Battery, or is it on a wait() status since it recieves a message?
I read the Android API about Services but for me, it seems that they only describe to Access the Service from the Activity, and not the other way round.
If I call a method of my Service within my Activity over the ServiceBinder, which has a return value, but was started in another Thread in my Service, how will my Activity know that it processes finish? 'busy waiting' on a boolean member of my Service doesn't seem to be the way to go. If I do that, like I saw in a tutorial, I don't see a reason not to do the work in the Activity Thread itself.
I read also about an AsyncTask, but this doesn't seem to be practicable for me, because I have to do different work related to JSON Objects I get.
Please you expirienced Android guys: show me the way.
You probably don't need a full-fledged service if you just need a background thread. Services can run independent of an activity and reconnect, etc. If you don't intend for the thread to be stand-alone, then just use a class that implements Runnable. Since you seem to have a handle (no pun intended) on handlers, this won't be too hard. Just post a message to your handler from your thread to let the activity know what's going on.

send intent only if activity is already running

Hello,
I have a service that is running at all times. When it receives a certain intent I want it to pass this along to an activity that I have so that the activity can be updated. However, I don't want to resume the activity if it is stopped/paused. If the activity is stopped, I want it to stay that way and then I'll just check my content provider when the user chooses to return to that activity. Sending this intent seems to automatically end up calling onStart and onResume and bring the activity back into focus. So I'm wondering if there is a way to only send the intent if the activity is already running. Also, if this is not possible, are there any other suggestions you guys have?
I figured there would some flag I could add to the intent to make it do what I want but I haven't found anything useful.
There's even a question about this in the developer.android.com faq but the answer is not very helpful.
How can I check if an Activity is already running before starting it?
The general mechanism to start a new activity if its not running— or to bring the activity stack to the front if is already running in the background— is the to use the NEW_TASK_LAUNCH flag in the startActivity() call.
Click to expand...
Click to collapse
Than you so much,
Samuel Maskell
Edit: Removed explanation as to why the post is somewhat vague. See below.
You drew so much more attention to your potentially "secret" project by saying all that. If you had just given your question with a little context and not mentioned all that people would understand it and answer without thinking anything of it
well I wrote the post and felt that it was a little vague so I added a sentence to explain why. I'm not trying to draw attention to it. I don't feel like the post is hard to follow at all but perhaps I will remove the stuff about the project and keep it solely about the problem.
And please don't take this the wrong. I feel like what I just said sounds really bitter but I'm just trying to explain myself.

Kill the app...

I think I already know the answer to this, but just wanted to double-check before I code it.
I have a app that runs a count-down timer and reads GPS at every interval. If I want to add a "stop" button, coding a finish(); as we all know, won't destroy the app.
So, the question is, if I want a "true" stop button, I'll have to run the app as a Service, right?
TIA,
Roots
If you call finish() in the final activity, your onDestroy method will get called. Any cleanup (stopping the gps radio) should then be done.
The linux process for your application may still be running, however, if the Android system needs to reclaim the memory, then that process will also be killed.
Great, I'll give it a try.
Thank you
Roots
REPLY: Works as advertised, thank you very much. I'm going to run it as a Service as it is a "mission critical" app and I need more control then the standard UI thread offers.

How to let function wait AsyncTask finish then only perform return

How to let function wait AsyncTask finish then only perform return, purposely my function need to get a result processed from AsyncTask, any expert can help?
lye85 said:
How to let function wait AsyncTask finish then only perform return, purposely my function need to get a result processed from AsyncTask, any expert can help?
Click to expand...
Click to collapse
You cannot let a method running in the UI (main) Thread of your application wait for an Async task to finish, that would block the UI Thread and result in an "App is not responding" dialog. To use Async tasks and get the result, read this documentation, especially "The 4 steps". You'd have to use the onPostExecute(Result) method to get the result. You can then call the function which needs the result again. But never wait for an async task to finish, that's not the point of it!
I've just posted such a onPostExecute example here, seems this is exactly what you asked for by coincidence..
thanks for all reply, but is there any other to call ksoap without using Asynctask?
Android- Asynchronous
lye85 said:
How to let function wait AsyncTask finish then only perform return, purposely my function need to get a result processed from AsyncTask, any expert can help?
Click to expand...
Click to collapse
Here You have Many options with you like
1. Just Load Your required UI Page After the Post Execute Method
2. Just Show Dummy Text firstly then update the ui after you get your response.
3. Just take one flag which is updatinng on post execute method after that u can update your ui
First question would be - why are you using ASYNCtaks if what you require seems to be a sync task If you're using it because you want to change GUI when the task is finished, the easiest way would be of course to use onPostExecuted method of AsyncTask. The other way would be to supply a callback/runnable to your async task (but why would you do that when there's onPostExecuted is beyond my comprehension).
Note that on Android you have to think asynchronically. I've tried to wait for async tasks to finish, but believe me - it was not worth it and I was severely punished!

Intent filters

Ok so i used to be a very experienced actionscript user. But i recently wanted to get back into programming so im learning java now or rather trying to.
So im trying to understand the xml side of this so far and have some questions im hoping u guys can answer.
activities
So as i understand it an activity is a screen in an app. So for example the standard launcher on my phone has pages of apps on my main screens. Each one of those would be an activity? And then another activity for when i press the button to display all installed apps?
Have i understood that properly?
intent filters
This is where im getting confused. Ive googled it and read as much as i can find. As i read it, feel like someones explaining rocket science to a 5 year old.
The words component and instances is being thrown around enough that i got no clue whats even being said. What i have understood is it gives data to the activity.
So a best guess from my partwould be its telling it how the screens going to work or maybe like if theres going to be a button on it maybe. Or perhaps it means something different like just the properties sort of stuff like screen size or stuff of that nature.
As u guys can probably tell im confused beyond comprehension. I got no idea how good my grasp is of any of this. But if theres someone out there kind enough to dumb it down a bit so normal people can understand it. Id be very gratefull.
Bump
Bump
Hey,
unfortunately i cant answer your question. This is a part of android I don't understand too.
I think you should post your question again in the Question and Answer forum of XDA. http://forum.xda-developers.com/android/help maybe there is an developer who can help you.
Greetings
Thanks ill post there and see if it makes any difference
Android apps run in sandboxes. Consider a huge box partitioned into 'n' number of sections. Each partitions represent a place in android environment where app can reside. By sandboxing, app are given unique ID(UID) when they start a process. Only apps having same (UID) can access the app's resources so no malicious app can peek into the resources of other app.
If apps cannot see other resources, how could they communicate within apps? Thats where intent comes in. Intents are like post letters and android is the postman. You can either give:
1. Recipient address, here, the package name of the app for which the intent is to be sent and may include your data in the intent if there is any and the postman (android) delivers it to the right person (target app). This is also known as explicit intent because you know the target class to call
2. Intent filter - This is not as specific as the first one. Here, your app has to perform something using an external app but your app doesnt know of the other apps which could perform the action. So, this time it will go for a broadcast and say "Hey! I need to take a picture! All camera apps please come in" and the broadcast is sent by android which displays list of apps which respond to this broadcast. When the user chooses the app to perform the action, the app fires, performs the task for your app, return the data to your app in form of intent result.
To register an app to respond to this type of intent, the app specifies something called intent-filter in their manifest which says the android, in our example context "I can perform camera action". So next time intent for camera action is sent, the app is in the list of apps to perform action with. This type of intent in implicit intent as you let the android and user decide the app. You can also specify your own custom intent-filters apart from standard android ones.
vijai2011 said:
Android apps run in sandboxes. Consider a huge box partitioned into 'n' number of sections. Each partitions represent a place in android environment where app can reside. By sandboxing, app are given unique ID(UID) when they start a process. Only apps having same (UID) can access the app's resources so no malicious app can peek into the resources of other app.
If apps cannot see other resources, how could they communicate within apps? Thats where intent comes in. Intents are like post letters and android is the postman. You can either give:
1. Recipient address, here, the package name of the app for which the intent is to be sent and may include your data in the intent if there is any and the postman (android) delivers it to the right person (target app). This is also known as explicit intent because you know the target class to call
2. Intent filter - This is not as specific as the first one. Here, your app has to perform something using an external app but your app doesnt know of the other apps which could perform the action. So, this time it will go for a broadcast and say "Hey! I need to take a picture! All camera apps please come in" and the broadcast is sent by android which displays list of apps which respond to this broadcast. When the user chooses the app to perform the action, the app fires, performs the task for your app, return the data to your app in form of intent result.
To register an app to respond to this type of intent, the app specifies something called intent-filter in their manifest which says the android, in our example context "I can perform camera action". So next time intent for camera action is sent, the app is in the list of apps to perform action with. This type of intent in implicit intent as you let the android and user decide the app. You can also specify your own custom intent-filters apart from standard android ones.
Click to expand...
Click to collapse
Gave u a thanks fkr that very useful responce. Thank you. So if ive understood this right. If i want to take a picture for example the intent filter part basically calls out for an external app to perform the action. With intent filter so basically when an app asks u with that menu what app youd like to choose thats an intent filter? And implicent thats the same menu or android just chooses it for you? Im trying to picture it from the user-side
Recipient address seems straight forward. You know u need an app and what its called. So u dont even need the user to select anything.
scorpafied said:
Gave u a thanks fkr that very useful responce. Thank you. So if ive understood this right. If i want to take a picture for example the intent filter part basically calls out for an external app to perform the action. With intent filter so basically when an app asks u with that menu what app youd like to choose thats an intent filter? And implicent thats the same menu or android just chooses it for you? Im trying to picture it from the user-side
Recipient address seems straight forward. You know u need an app and what its called. So u dont even need the user to select anything.
Click to expand...
Click to collapse
Your understanding on implicit intent is correct. To make it even more short an precise,
An intent is an abstract definition of the operation to be performed by the app
Click to expand...
Click to collapse
When the target action is known, say you invoke a second activity of you app. You know the target action (the class name), that is your implicit intent. You decide what action to perform without user intervention here. Explicit usually needs user input for inter-app communication to take place.
Examples of few implicit intents:
Starting an activity
Starting a service
vijai2011 said:
Your understanding on implicit intent is correct. To make it even more short an precise,
When the target action is known, say you invoke a second activity of you app. You know the target action (the class name), that is your implicit intent. You decide what action to perform without user intervention here. Explicit usually needs user input for inter-app communication to take place.
Examples of few implicit intents:
Starting an activity
Starting a service
Click to expand...
Click to collapse
Ok cool. So recipient is when we choose the app. Intent is whwn they get a menu to choose it. And implicent is is when android chooses it for them.
Sounds simple enough. Thanks for all the help buddy
And would i be right in assuming that the actions we put in intent filters and the others in form it of stuff like when its triggered, how often and so forth?

Categories

Resources