【问题标题】:I'm Wondering Thread flow between activity and service in android我想知道android中活动和服务之间的线程流
【发布时间】:2015-01-03 00:40:41
【问题描述】:

当我从活动启动服务时, 这是不是主线程?

我想知道如果我激活了服务,为什么我的系统太慢了。 所以我在想该服务可能会使用主线程。

[Service] - 主作业 - onStartCommand

for(int i = 0; i < hp.length; i++){
   if(i%10 == 0 && i != 0){
    Log.i(TAG, "total data : "+hp.length);
   }
}                   
stopSelf();

我是否在服务流中使用另一个线程? 给我一个建议。

谢谢:)

【问题讨论】:

  • 您可以通过在 logcat 消息中包含线程名称自行确定。

标签: android multithreading performance android-activity service


【解决方案1】:

如此处所述https://developer.android.com/guide/components/services.html

Service This is the base class for all services. When you extend this class, 创建一个新线程来完成所有服务的工作很重要, because the service 使用应用程序的主线程, by default, which could slow the performance of any activity your application is running.

IntentService This is a subclass of Service that uses a worker thread to handle all start requests, one at a time.如果您不要求您的服务同时处理多个请求,这是最佳选择。All you need to do is implement onHandleIntent(), which receives the intent for each start request so you can do the background work.

【讨论】:

    猜你喜欢
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多