0

我试图弄清楚 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执行后执行,根据 MainActivity 的顺序应该更早启动并并行执行?有人可以解释一下吗?23:00:07.502IntentService

代码为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;
    }
}

我替换IntentServiceThread如下

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");

        new ChkServiceThread().start();

        //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();
    }

    private class ChkServiceThread extends Thread {

        @Override
        public void run() {

            boolean isEmpty = IsContactEmpty.valueOf(MainActivity.this);

            if ( isEmpty == true ) {
                Log.d(TAG, "Contacts Empty");

            } else {
                Log.d(TAG, "Contacts not Empty");
                // mHandler.post(new DisplayToast("Contacts Ok", this));
            }

        }

    }

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

4

1 回答 1

1

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

于 2012-08-29T18:18:16.863 回答