Android 服务不会立即启动

发布于 2024-11-01 22:27:32 字数 2517 浏览 0 评论 0原文

public class TestService extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Intent service=new Intent(getApplicationContext(),MessageListener.class);

        Log.v("Test", "Going to start service");    
        startService(service);
        Log.v("Test", "service started?");

    }
}

public class MessageListener extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.v("Test", "Start Cmd");
        intent.setAction("Started");
        new Thread(new Runnable() {

            @Override
            public void run() {
                for(int i=100;i<200;i++){
                    Log.v("Test",i+"");
                }

            }
        }).start();
        return START_STICKY;

    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.v("Test", "Create");
    }

我希望它会打印:

Start Service
create
Start cmd
print 1->100
Service Started.

但我得到了

Start Service
Service Started.
create 
Start cmd
prints 1->100

为什么是这样?


我发现问题是由于异步造成的。 startService 将在父方法完成后被调用。 解决方案是:

public class TestService extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       Intent service=new Intent(getApplicationContext(),MessageListener.class);

               startService(service);

               mCheckerHandler.sendEmptyMessageDelayed(MSG_CHECK_SERVICE_RUNNING, 100);
   }

   private static final int MSG_CHECK_SERVICE_RUNNING = 0x001122;

   private Handler mCheckerHandler = new Handler() {
           public void handleMessage(android.os.Message msg) {
                   if (msg.what == MSG_CHECK_SERVICE_RUNNING) {
                           if (checkServiceRunning()) {
                                   //Do something
                           } else {
                                   //Send another message to check in the next 100ms
                                   sendEmptyMessageDelayed(MSG_CHECK_SERVICE_RUNNING, 100);
                           }
                   }
           };
   };
}

谢谢大家。特别是对平先生:)

public class TestService extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Intent service=new Intent(getApplicationContext(),MessageListener.class);

        Log.v("Test", "Going to start service");    
        startService(service);
        Log.v("Test", "service started?");

    }
}

public class MessageListener extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.v("Test", "Start Cmd");
        intent.setAction("Started");
        new Thread(new Runnable() {

            @Override
            public void run() {
                for(int i=100;i<200;i++){
                    Log.v("Test",i+"");
                }

            }
        }).start();
        return START_STICKY;

    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.v("Test", "Create");
    }

I expect it would print:

Start Service
create
Start cmd
print 1->100
Service Started.

But I'm getting

Start Service
Service Started.
create 
Start cmd
prints 1->100

Why is it?


I've found the problem is due to Asynchronous. startService will be called after parent's method finished.
The solution is:

public class TestService extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       Intent service=new Intent(getApplicationContext(),MessageListener.class);

               startService(service);

               mCheckerHandler.sendEmptyMessageDelayed(MSG_CHECK_SERVICE_RUNNING, 100);
   }

   private static final int MSG_CHECK_SERVICE_RUNNING = 0x001122;

   private Handler mCheckerHandler = new Handler() {
           public void handleMessage(android.os.Message msg) {
                   if (msg.what == MSG_CHECK_SERVICE_RUNNING) {
                           if (checkServiceRunning()) {
                                   //Do something
                           } else {
                                   //Send another message to check in the next 100ms
                                   sendEmptyMessageDelayed(MSG_CHECK_SERVICE_RUNNING, 100);
                           }
                   }
           };
   };
}

Thanks for all of you. Especially to Mr Binh :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖鼻大婶 2024-11-08 22:27:32

这是因为线程以“伪”并行方式执行,因此在计数器线程获得任何 CPU 时间进行写入之前调用 Log.v("Test", "service started?");

“伪”并行是因为大多数手机没有超过 1 个 CPU,因此它们无法并行计算,因此只能从一个线程切换到另一个线程。您可以在 Wikipedia 或您喜欢的任何其他来源上阅读有关线程的更多信息。

That's because the thread is executing in "pseudo"-parallel so the Log.v("Test", "service started?"); is called before the counter-thread gets any CPU-time to write.

"Pseudo"-parallel because most phones don't have more than 1 CPU so they cannot compute in parallel so they only switch from one thread to another. You can read more about threading on Wikipedia or any other source you like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文