Created
November 22, 2011 08:51
-
-
Save neiraza/1385224 to your computer and use it in GitHub Desktop.
hoge.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* Copyright (C) 2007 The Android Open Source Project | |
package jp.co.fttx.rakuphotomail.activity; | |
import jp.co.fttx.rakuphotomail.R; | |
import jp.co.fttx.rakuphotomail.service.HogeService; | |
import android.app.Activity; | |
import android.content.BroadcastReceiver; | |
import android.content.ComponentName; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.content.ServiceConnection; | |
import android.os.Bundle; | |
import android.os.IBinder; | |
import android.util.Log; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class HogeActivity extends Activity implements OnClickListener { | |
private static final String ACTION = "HogeActivity.ACTION"; | |
private TextView mResultText; | |
private Button mStart; | |
private Button mStop; | |
private Button mResult; | |
private Intent intent = new Intent(this, HogeService.class); | |
private HogeReceiver receiver = new HogeReceiver(); | |
private HogeService hogeService; | |
private boolean mIsBound = false; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
Log.d("hoge", "HogeActivity#onCreate"); | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.hoge); | |
mResultText = (TextView) findViewById(R.id.hoge_text_result); | |
mStart = (Button) findViewById(R.id.hoge_start); | |
mStart.setOnClickListener(this); | |
mStop = (Button) findViewById(R.id.hoge_stop); | |
mStop.setOnClickListener(this); | |
mResult = (Button) findViewById(R.id.hoge_result); | |
mResult.setOnClickListener(this); | |
} | |
@Override | |
public void onStart() { | |
Log.d("hoge", "HogeActivity#onStart"); | |
super.onStart(); | |
doBind(); | |
} | |
private void doBind() { | |
Log.d("hoge", "HogeActivity#doBind"); | |
if (!mIsBound) { | |
mIsBound = bindService(intent, mConnection, | |
Context.BIND_AUTO_CREATE); | |
} | |
} | |
private void doUnBind() { | |
Log.d("hoge", "HogeActivity#doUnBind"); | |
// XXX | |
} | |
@Override | |
public void onDestroy() { | |
Log.d("hoge", "HogeActivity#onDestroy"); | |
if (mIsBound) { | |
mIsBound = false; | |
doUnBind(); | |
} | |
super.onDestroy(); | |
} | |
private ServiceConnection mConnection = new ServiceConnection() { | |
public void onServiceConnected(ComponentName className, IBinder service) { | |
Log.d("hoge", "ServiceConnection#onServiceConnected"); | |
hogeService = ((HogeService.HogeBinder) service).getService(); | |
} | |
public void onServiceDisconnected(ComponentName className) { | |
Log.d("hoge", "ServiceConnection#onServiceDisconnected"); | |
hogeService = null; | |
} | |
}; | |
private void doServiceStart() { | |
Log.d("hoge", "HogeActivity#doServiceStart"); | |
intent.putExtra("message", "計算開始!"); | |
startService(intent); | |
IntentFilter filter = new IntentFilter(HogeActivity.ACTION); | |
registerReceiver(receiver, filter); | |
} | |
private void doServiceStop() { | |
Log.d("hoge", "HogeActivity#doServiceStop"); | |
unregisterReceiver(receiver); | |
hogeService.stopSelf(); | |
} | |
private void doResult() { | |
Log.d("hoge", "HogeActivity#doResult"); | |
int r = hogeService.getResult(); | |
Log.d("hoge", "HogeActivity#doResult r:" + r); | |
mResultText.setText(r); | |
} | |
@Override | |
public void onClick(View v) { | |
switch (v.getId()) { | |
case R.id.hoge_start: | |
Log.d("hoge", "HogeActivity#onClick start"); | |
doServiceStart(); | |
break; | |
case R.id.hoge_stop: | |
Log.d("hoge", "HogeActivity#onClick stop"); | |
doServiceStop(); | |
break; | |
case R.id.hoge_result: | |
Log.d("hoge", "HogeActivity#onClick result"); | |
doResult(); | |
break; | |
default: | |
break; | |
} | |
} | |
private class HogeReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Log.d("hoge", "HogeReceiver#onReceive"); | |
Toast toast = Toast.makeText(getApplicationContext(), "result:" | |
+ intent.getStringExtra("result"), Toast.LENGTH_LONG); | |
toast.show(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.co.fttx.rakuphotomail.service; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.os.Binder; | |
import android.os.IBinder; | |
import android.util.Log; | |
import android.widget.Toast; | |
public abstract class HogeService extends Service { | |
private int result = 0, tmp = 0; | |
private Thread fibonacciThread; | |
private Runnable fibonacci; | |
private boolean roop; | |
private String message; | |
@Override | |
public void onCreate() { | |
Log.d("hoge", "HogeService#onCreate"); | |
super.onCreate(); | |
result = 0 + 1; | |
} | |
@Override | |
public void onStart(Intent intent, int startId) { | |
Log.d("hoge", "HogeService#onStart"); | |
message = intent.getStringExtra("message"); | |
Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); | |
fibonacci = new Runnable() { | |
@Override | |
public void run() { | |
while (roop) { | |
Log.d("hoge", "計算中..."); | |
tmp = result; | |
result = result + tmp; | |
try { | |
Thread.sleep(10000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
}; | |
fibonacciThread = new Thread(fibonacci); | |
fibonacciThread.start(); | |
} | |
@Override | |
public void onDestroy() { | |
Log.d("hoge", "HogeService#onDestroy"); | |
} | |
public class HogeBinder extends Binder { | |
public HogeService getService() { | |
Log.d("hoge", "HogeBinder#getService"); | |
return HogeService.this; | |
} | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
Log.d("hoge", "HogeService#onBind"); | |
return new HogeBinder(); | |
} | |
public int getResult() { | |
Log.d("hoge", "HogeService#getResult"); | |
return result; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:orientation="vertical" > | |
<TextView | |
android:text="Result:" | |
android:textSize="50sp" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" /> | |
<TextView | |
android:id="@+id/hoge_text_result" | |
android:textSize="50sp" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:layout_weight="8" /> | |
<LinearLayout | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" > | |
<Button | |
android:id="@+id/hoge_start" | |
android:layout_width="wrap_content" | |
android:layout_height="fill_parent" | |
android:text="START" | |
android:layout_weight="5" /> | |
<Button | |
android:id="@+id/hoge_stop" | |
android:layout_width="wrap_content" | |
android:layout_height="fill_parent" | |
android:text="STOP" | |
android:layout_weight="5" /> | |
<Button | |
android:id="@+id/hoge_result" | |
android:layout_width="wrap_content" | |
android:layout_height="fill_parent" | |
android:text="RESULT" | |
android:layout_weight="5" /> | |
</LinearLayout> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment