Praktikum 7 | Thread
Buatlah project baru seperti ketentuan berikut :
Project Name : Thread
Buitl Target : Android 2.2
Application name : Thread
Package name : www.percobaan2.com
Activity : database4
Buka file values/string.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Aplysit - IT Solution Center</string>
<string name="app_name">Try Thread - www.aplysit.com</string>
</resources>
Selanjutnya main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="Start"
android:layout_height="wrap_content"
android:id="@+id/btn_start"
android:layout_width="wrap_content">
</Button>
<Button android:text="Stop"
android:layout_height="wrap_content"
android:id="@+id/btn_stop"
android:layout_width="wrap_content">
</Button>
</LinearLayout>
Mythread.java :
package www.percobaan2.com;
import android.content.Context;
import android.os.Handler;
public class Mythread {
public Handler mhandler;
public showText text;
public Mythread (Context context, Handler handler){
mhandler = handler;
}
public synchronized void startShowText() {
if (text == null) {
text = new showText();
text.start();
}
}
public synchronized void stopShowText() {
if (text != null) {
text.requestStop();
text = null;
}
}
private class showText extends Thread {
private volatile boolean stop = false;
public void run() {
while ((!stop)){
mhandler.obtainMessage(threadsimple.run,"Pesan dari thread" + Math.random()).sendToTarget();
try {
sleep(3000);
}
catch (Exception e){}
}
if (stop) {
mhandler.obtainMessage(threadsimple.stop).sendToTarget();
}
}
public synchronized void requestStop() {
stop = true;
}
}
}
threadsimple.java :
package www.percobaan2.com;
import www.percobaan2.com.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class threadsimple extends Activity {
public static final int run = 1;
public static final int stop = 2;
public Mythread thread;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button start = (Button)findViewById(R.id.btn_start);
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
thread.startShowText();
System.out.println("run");
}
});
Button stop = (Button)findViewById(R.id.btn_stop);
stop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
thread.stopShowText();
System.out.println("stop");
}
});
}
public void onStart() {
super.onStart();
thread = new Mythread(this, mHandler);
}
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case run:
Toast.makeText(getApplicationContext(), msg.obj.toString(), 2).show();
break;
case stop:
Toast.makeText(getApplicationContext(), "Thread Stop", 2).show();
break;
}
}
};
}
Hasil Running :
- Praktikum 7 | Thread
- Unknown
- Senin, 15 Juni 2015
- Tidak ada komentar:
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar