Created
April 9, 2013 13:21
-
-
Save amay077/5345646 to your computer and use it in GitHub Desktop.
bind されてても stopService で止まるサービスの作り方 ref: http://qiita.com/items/08f255bc90b99969bb72
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
| 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); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
why "not BIND_AUTO_CREATE" ?