Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
997 views
in Technique[技术] by (71.8m points)

android - Execute long running operations on the service

I'm building an application which have a Service. I know that all application components run in the same UI process, at least you specify it in the manifest. So to avoid ANR's messages i have three ways.

  1. Specify the service in the manifest to run in a separate process like android:process=":remote" but i've read some StackOverflow's post that
    says that it's not a good idea, because it consume a lot of battery and cpu processing. That i really respect since those post are from trusted people.

  2. Use an IntentService. it's probably a good way out. but i need my service running even if the activity isn't visible. Because i need the service keep checking against a web service for new messages from other users and notify thru Notification. Could it be posible using a an IntentService? is that an ellegant solution.

  3. Use a local service. just removing the android:process=":remote" attribute from the manifest file. But i get some ...OnMainThreadException errors. it means that i need to create an special thread to execute those long running operations or use AsyncTask,

maybe there are another ways to do it. please let me know, how to execute long runnig operations on the service. is really imperative.

thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Android Service executing in UI thread. So you should use AsyncTask or another way to work with threads for network requests.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...