开发者

Background Service in Android - Need Help

开发者 https://www.devze.com 2023-03-15 18:12 出处:网络
I need some help or suggestions regarding Background services. Well I want to achieve this. I have an application with Some Views that application also has a Background Service that always keeps on r

I need some help or suggestions regarding Background services.

Well I want to achieve this. I have an application with Some Views that application also has a Background Service that always keeps on running.

In my views there is a Button whenever I press that 开发者_开发百科button, that button passes some files to the Background Service and my Background service upload that file onto some server.

I am done with the uploading process. Now I want to know that how can I make a Background Service that always keeps on running and on my tapping of the button it sends a file to the Service.

I am new in Background service implementation.

Please guide Friends with some tutorials, suggestions or guidelines.

Thanks a bunch


You've probably already read some of the Android Service documentation, but I suggest studying it further and looking at the Local Service Sample if you have not done so already:

http://developer.android.com/reference/android/app/Service.html

It sounds like you have already got your Service up and running, and I think the actual problem you are trying to solve now is how to communicate data from your Activity to your Service. When your Activity is bound with a Service that's part of the same application, that service is in the same process and runs on the same main UI thread, so once you get the IBinder object from the Service after binding with it, you can simply directly call the functions in that Service from your Activity. Similarly, you can pass your Service a handler object from your Activity so that the service can send messages or post Runnables to your Activity. Communication with a local Service is therefore quite simple.

So if you take a look at the Local Service Sample in the link above, you will see a section in the code where we get a reference to the Service once binding has completed:

mBoundService = ((LocalService.LocalBinder)service).getService();

After that point, it's possible to directly call methods on that Service that's in the same application. For example, you could have a method called sendFile in your Service. In your Activity, you might do something like:

mBoundService.sendFile( myStuffObject );

There are quite a number of questions on Stack Overflow regarding communicating between an Activity and a Service, and I think you'd find it beneficial to search and read these.


A standard Android service will do just fine in this case. It will continue running in the background untill its work is finished or until you ask it to stop. There is a topic on the android dev site explaining services in detail.


you should go for android Service that is used for Background operation . Inside the service your have use TimerTask which will be checking the Queue for every x sec and when any items present in the Queue it will pull the item and upload it to the server. here is the link for Android Service.. http://developer.android.com/reference/android/app/Service.html

Link fro Queue.http://developer.android.com/reference/java/util/Queue.html

0

精彩评论

暂无评论...
验证码 换一张
取 消