PortableAndroidDeveloerEnviroment?
import android.widget.Button;が増えるのでエラーが出なくなる
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(HelloWorldActivity.this, "Hello!", Toast.LENGTH_LONG).show();
}
});res-values-strings.xmlをディレクトリごとコピー valuesとvalues-jaとの二つにする。 values-ja/strings.xmlを日本語に書き換える
Toast行を以下に入れ替え
new Thread() {
@Override
public void run() {
try{
Thread.sleep(10 * 1000);
} catch (InterruptedException e){
e.printStackTrace();
}
handler.post(new Runnable(){
public void run() {
Toast.makeText(HelloWorldActivity.this,"Hello!", Toast.LENGTH_LONG).show();
}
});
}}.start();
ハンドラを作っておく
public class HelloPADE2Activity extends Activity {
の下に一行追加
private Handler handler = new Handler();
Ctl+Shift+Oでimportを増やしておく
public class HelloPADEActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// new Thread() {
// @Override
// public void run() {
// Log.v("hello", "Error");
// try {
// new NumberFormatException();
// Thread.sleep(10 * 1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// handler.post(new Rnnable() {
// public void run() {
// Toast.makeText(HelloWorldActivity.this,
// "Hello!", Toast.LENGTH_LOGNG).show();
// }
// });
// }
// }.start();
EditText text = (EditText) findViewById(R.id.editText1);
String value = text.getText().toString();
Intent intent = new Intent(HelloWorldActivity.this,
SecondActivity.class);
intent.putExtra("text",value);
startActivityForResult(intent, 0);
}
});
}
@Override
protected void on ActibityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode ==1) {
String value = data.getExtras().getString("result");
Toast.makeText(this, value, Toast.LENGTH_LONG).show();
}
}
}
package jp.cane.android.sweet;
import android.app.Activityl
import android.content.Intent;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent intent = getIntent();
String value = intent.getExtras().getString("text");
// Toast.makeText(this,value,Toast.LENGTH_LONG.show();
value = "こんにちは、" + value;
Intent data = new Intent();
data.putExtra("result",value);
setResult(1);
finish();
}
}
package jp.cane.android.sweet;
import android.app.Activityl
import android.content.Intent;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent intent = getIntent();
String value = intent.getExtras().getString("text");
// Toast.makeText(this,value,Toast.LENGTH_LONG.show();
value = "こんにちは、" + value;
Intent data = new Intent();
data.putExtra("result",value);
setResult(1);
finish();
}
}
New-classで、MainService?.java できたらextendedのサービスを追加 を以下のように作る package jp.cone.android.sweet;{
import android.app.Service; import android.content.Intent; import android.os.IBinder;
public class MainService extends Service {
@Override
public void onCreate()
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
その次に、以下のように拡張する
public class MainService extends Service implements Runnable {
private Handler handler = new Handler();
@Override
public void onCreate()
super.onCreate();
new Thread(this).start();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void run() {
while (true) {
try {
Thread.sleep(3 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
public void run() {
Toast.makeText(MainService.this, "Hello", Toast.LENGTH_LONG).show();
}
});
}
}
}
このサービスは問題があって、AndroidOSから勝手にkillされる。それを防ぐためにはstartforgroundとかをやると終了されなくなる。
MainService?.javaを書き換え
public void run() {
handler.post(new Runnable() {
public void run() {
adapter = BluetoothAdapter.getDefaultAdapter();
}
});
while (adapter == null) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while (true)
// try {
// Tread.sleep(3 * 1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// handler.post(new Runnable() {
// public void run() {
// Toast.makeText(MainService.this, "Hello", Toast.LENGTH_LONG).show();
// }
// });
try {
BluetoothSocket socket;
if (true) { // server
BluetoothServerSocket ssocket = adapter
.listenUsingRfcommWithServiceRecord("BTChat",
UUID.fromString(UI_MODE_SERVICE));
socket = ssocket.accept();
} else { // client
socket = adapter.getRemoteDevice("00:11:22:33:44:55")
.createRfcommSockeToServiceRecord(
UUID.fromString(UI_MODE_SERVICE));
socket.connect();
}
// if server , move below one line to "point B".
socket.getOutputStream().write("hogehoge\n".getBytes());
InputStream is = socket.getInputStream();
BufferedReader reader = new BufferdReader(
new InputStreamReader(is));
final String line = reader.readLine();
handler.post(new Runnable() {
public void run() {
Toast.makeText(MainService.this, line,
Toast.LENGTH_LONG).show();
}
});
// point B
} catch (IOException e) {
// TODO Auto-genereted catch block
e.printStackTrace
}
パーミッションを追加する必要がある res-AndroidManifest?.xmlのAndroid Manifest Permissionsでon.BLUETOOTH
on.BLUETOOTH_ADMIN
アクティビティを追加する度に