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.502
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
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 出力とまったく同じままです。