Android 开发教程之 Service 详解
2010-02-22 16:14:00 来源:WEB开发网8. import android.os.Bundle;
9. import android.os.IBinder;
10. import android.view.View;
11. import android.view.View.OnClickListener;
12. import android.widget.Button;
13. import android.widget.Toast;
14.
15. public class TestServiceHolder extends Activity {
16. private boolean _isBound;
17. private TestService _boundService;
18.
19. public void onCreate(Bundle savedInstanceState) {
20. super.onCreate(savedInstanceState);
21. setContentView(R.layout.test_service_holder);
22. setTitle("Service Test");
23.
24. initButtons;
25. }
26.
27. private ServiceConnection _connection = new ServiceConnection {
28. public void onServiceConnected(ComponentName className, IBinder service) {
29. _boundService = ((TestService.LocalBinder)service).getService;
30.
31. Toast.makeText(TestServiceHolder.this, "Service connected",
32. Toast.LENGTH_SHORT).show;
33. }
34.
35. public void onServiceDisconnected(ComponentName className) {
36. // unexpectedly disconnected,we should never see this happen.
37. _boundService = null;
38. Toast.makeText(TestServiceHolder.this, "Service connected",
39. Toast.LENGTH_SHORT).show;
40. }
41. };
42.
43. private void initButtons {
44. Button buttonStart = (Button) findViewById(R.id.start_service);
45. buttonStart.setOnClickListener(new OnClickListener {
46. public void onClick(View arg0) {
47. startService;
48. }
49. });
50.
51. Button buttonStop = (Button) findViewById(R.id.stop_service);
52. buttonStop.setOnClickListener(new OnClickListener {
更多精彩
赞助商链接