Skip to content

Instantly share code, notes, and snippets.

@amay077
Created April 9, 2013 13:21
Show Gist options
  • Select an option

  • Save amay077/5345646 to your computer and use it in GitHub Desktop.

Select an option

Save amay077/5345646 to your computer and use it in GitHub Desktop.
bind されてても stopService で止まるサービスの作り方 ref: http://qiita.com/items/08f255bc90b99969bb72
Intent intent = new Intent(context, TestService.class);
final ServiceConnection conn = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("ServiceTest", "onServiceConnected");
binder = ITestService.Stub.asInterface(service);
}
// stopService すると呼ばれる
public void onServiceDisconnected(ComponentName name) {
Log.i("ServiceTest", "onServiceDisconnected");
context.unbindService(conn); // これ呼ばないとリークする
}
};
// サービス開始&接続
context.startService(intent);
context.bindService(intent, conn, 0); // not BIND_AUTO_CREATE
// ボタンを押したらサービス停止
findViewById(R.id.btnCallTerminate)
.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
context.stopService(intent);
}
});
@ahmadmust8
Copy link
Copy Markdown

why "not BIND_AUTO_CREATE" ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment