class MainActivity
package org.androidtown.mythread;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String TAG="MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButton1Clicked(View v){
Log.d(TAG, "첫번째 버튼 클릭됨 : " );
RequestThead thead =new RequestThead();
thead.start();
Toast.makeText(getApplicationContext(), "스레드 시작됨", Toast.LENGTH_SHORT).show();
}
class RequestThead extends Thread{
public void run(){
for(int i=0; i<100; i++){
println("#" + i + " : 호출됨 ");
try{
Thread.sleep(500);
}catch (Exception e) {
e.printStackTrace();
}
}
}
public void println(String data){
Log.d(TAG, data);
}
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.androidtown.mythread.MainActivity">
<Button
android:text="스레드시작"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/button"
android:onClick="onButton1Clicked"
/>
</RelativeLayout>
79강
댓글 ( 4)
댓글 남기기