开发者

Android service does not start immediately

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 :)


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜