是否可以通过Google Cloud Messaging避免使用thread.sleep()?

时间:2015-01-24 21:59:51

标签: android google-cloud-messaging

Google提供的链接https://developer.android.com/google/gcm/client.html说明了如何在Android应用中实施Google Cloud Messaging。

特别是,在处理消息接收的服务中,在onHandleIntent方法中,教程使用以下代码:

else if (GoogleCloudMessaging.
                MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            // This loop represents the service doing some work.
            for (int i=0; i<5; i++) {
                Log.i(TAG, "Working... " + (i+1)
                        + "/5 @ " + SystemClock.elapsedRealtime());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                }
            }
            Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
            // Post notification of received message.
            sendNotification("Received: " + extras.toString());
            Log.i(TAG, "Received: " + extras.toString());
        }

这会强制应用程序在发布通知之前等待25秒。 为什么教程会使用它?真的有必要吗?

1 个答案:

答案 0 :(得分:2)

这是因为上面的评论中所说的原因!

This loop represents the service doing some work.

这是示例代码,模拟该部分正在进行的一些实际工作。