什么时候(时间)Intentservice开始?

时间:2012-08-29 18:08:44

标签: android android-activity android-service

我试图弄清楚Android中的指令流程,并编写了以下代码

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "This is the ------------------- Start Line");
        Intent intent = new Intent(MainActivity.this, TimeCheckService.class);
        startService(intent);
        for ( int i = 0; i < 10; i++ ) {
            Log.d(TAG, "This line is to check the________________________ sequence of execeution in Android.");
        }
        Cursor cursor = getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        cursor.close();
    }

logcat输出

08-29 23:00:05.742: D/MainActivity(1462): This is the ------------------- Start Line
08-29 23:00:05.742: D/MainActivity(1462): This line is to check the________________________ sequence of execeution in Android.
08-29 23:00:05.742: D/MainActivity(1462): This line is to check the________________________ sequence of execeution in Android.
08-29 23:00:05.742: D/MainActivity(1462): This line is to check the________________________ sequence of execeution in Android.
08-29 23:00:05.742: D/MainActivity(1462): This line is to check the________________________ sequence of execeution in Android.
08-29 23:00:05.742: D/MainActivity(1462): This line is to check the________________________ sequence of execeution in Android.
08-29 23:00:05.742: D/MainActivity(1462): This line is to check the________________________ sequence of execeution in Android.
08-29 23:00:05.742: D/MainActivity(1462): This line is to check the________________________ sequence of execeution in Android.
08-29 23:00:05.742: D/MainActivity(1462): This line is to check the________________________ sequence of execeution in Android.
08-29 23:00:05.742: D/MainActivity(1462): This line is to check the________________________ sequence of execeution in Android.
08-29 23:00:05.742: D/MainActivity(1462): This line is to check the________________________ sequence of execeution in Android.
08-29 23:00:07.502: D/IsContactEmpty(1462): Starting IsContactEmpty
08-29 23:00:09.162: D/IsContactEmpty(1462): End IsContactEmpty
08-29 23:00:09.162: D/TimeCheckService(1462): Contacts not Empty

现在,logcat中的时间戳显示在执行 MainActivity 中的所有代码后IntentService执行了23:00:07.502,其中根据MainActivity {{ 1}}应该早点开始并且并行执行?有人可以解释一下吗?

IntentService

的代码
IsContactEmpty

我将public class IsContactEmpty { public static final String TAG = "IsContactEmpty"; @SuppressWarnings("unused") private void isContactsEmpty() { } public static boolean valueOf(Context context) { Log.d(TAG, "Starting IsContactEmpty"); boolean isEmpty; Cursor cursor = context.getContentResolver().query( ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (cursor.getCount() > 0) { isEmpty = false; } else { isEmpty = true; } cursor.close(); cursor = null; Log.d(TAG, "End IsContactEmpty"); return isEmpty; } } 替换为IntentService,如下所示

Thread

并运行应用程序10次但结果仍然与之前的logcat输出完全相同。

1 个答案:

答案 0 :(得分:1)

您的IntentServiceMainActivity在同一过程中运行。活动和服务中的所有生命周期调用(如onCreate()onResume()onStartCommand()都在主线程上运行。因此,在onCreate()调用MainActivity完成之前,主线程上的服务中不可能发生任何事情。