Created
August 29, 2012 08:13
-
-
Save granoeste/3508387 to your computer and use it in GitHub Desktop.
[Android] Service Bind/Unbind
This file contains 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
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" > | |
<Button | |
android:id="@+id/button1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentTop="true" | |
android:layout_centerHorizontal="true" | |
android:layout_marginTop="35dp" | |
android:text="bindService" /> | |
<Button | |
android:id="@+id/button2" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/button1" | |
android:layout_centerHorizontal="true" | |
android:text="ubindService" /> | |
<Button | |
android:id="@+id/button5" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/button2" | |
android:layout_centerHorizontal="true" | |
android:text="execute" /> | |
<Button | |
android:id="@+id/button3" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/button5" | |
android:layout_centerHorizontal="true" | |
android:text="start" /> | |
<Button | |
android:id="@+id/button4" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/button3" | |
android:layout_centerHorizontal="true" | |
android:text="stop" /> | |
</RelativeLayout> |
This file contains 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
public class MainActivity extends Activity { | |
private static final String TAG = MainActivity.class.getSimpleName(); | |
private final MainActivity self = this; | |
MainService mService; | |
MyServiceConnection mConn; | |
boolean mIsBound; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
findViewById(R.id.button1).setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent service = new Intent(self, MainService.class); | |
// ServiceConnection インスタンス作成 | |
mConn = new MyServiceConnection(); | |
// サービスをバインド | |
bindService(service, mConn, Context.BIND_AUTO_CREATE); | |
} | |
}); | |
findViewById(R.id.button2).setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (mIsBound) { | |
unbindService(mConn); | |
mIsBound = false; | |
// サービスをアンバインドしても、サービスは終了しない。 | |
// バインド時に使用した ServiceConnectionインスタンスでアンバインドを行う。 | |
// 異なるインスタンスでアンバインドすると例外が発生する。 | |
// バインドを繰り返す場合は、事前にアンバインドする。 | |
} | |
} | |
}); | |
findViewById(R.id.button3).setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent service = new Intent(self, MainService.class); | |
startService(service); | |
} | |
}); | |
findViewById(R.id.button4).setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent service = new Intent(self, MainService.class); | |
stopService(service); | |
} | |
}); | |
findViewById(R.id.button5).setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (mService != null) | |
mService.execute(); | |
} | |
}); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.activity_main, menu); | |
return true; | |
} | |
class MyServiceConnection implements ServiceConnection { | |
private final String TAG = MainActivity.MyServiceConnection.class.getSimpleName(); | |
@Override | |
public void onServiceConnected(ComponentName name, IBinder service) { | |
Log.d(TAG, "onServiceConnected()"); | |
mService = ((MainService.LocalBinder) service).getService(); | |
mIsBound = true; | |
} | |
@Override | |
public void onServiceDisconnected(ComponentName name) { | |
Log.d(TAG, "onServiceDisconnected()"); | |
mService = null; | |
mIsBound = false; | |
} | |
} | |
} |
This file contains 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
public class MainService extends Service { | |
@SuppressWarnings("unused") | |
private static final String TAG = MainService.class.getSimpleName(); | |
private final MainService self = this; | |
public class LocalBinder extends Binder { | |
MainService getService() { | |
Log.d(TAG, "getService()"); | |
return MainService.this; | |
} | |
} | |
private final IBinder mBinder = new LocalBinder(); | |
@Override | |
public IBinder onBind(Intent intent) { | |
Log.d(TAG, "onBind()"); | |
return mBinder; | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
Log.d(TAG, "onCreate()"); | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
Log.d(TAG, "onStartCommand()"); | |
Log.i(TAG, "Received start id " + startId + ": " + intent); | |
execute(); | |
return START_STICKY; | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
Log.d(TAG, "onDestroy()"); | |
} | |
@Override | |
public boolean onUnbind(Intent intent) { | |
Log.d(TAG, "onUnbind()"); | |
return true; | |
} | |
@Override | |
public void onRebind(Intent intent) { | |
Log.d(TAG, "onRebind()"); | |
super.onRebind(intent); | |
} | |
public void execute() { | |
Log.d(TAG, "execute() " + this.hashCode()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment