Vxu

i-M& t/jik&tj J IfciHffl«»»«^ Service .

Service ^SBSttit^tt^ onCreate W onStait ffiS&ftffiJ

- HSMW/flfi' TestServiceHolder, CP-Jin F:

package com.iceskysl.TestServiceHolder;

import Android.app.Activity;

import Android.content.ComponentName;

import Android.content.Context;

import Android.content.Intent;

import Android.content.Servi ceConnecti on;

import Android.os.Bundle;

import Android.os.IBinder;

import Android.view.View;

import Android.view.View.OnClickListener; import Android.widget.Button; import Android.widget.Toast;

public class TestServiceHolder extends Activity {

private boolean isBound;

private TestService boundService;

/** Called when the activity is first created. */ @0verride public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); setContentView (R. layout. main) ; setTitle ("Service Test");

private ServiceConnection connection = new ServiceConnection () { public void onServiceConnected(ComponentName className, IBinder Service) { boundService = ((TestService.LocalBinder)Service).getServiceO;

Toast.makeText(TestServiceHolder.this, "Service connected",

Toast.LENGTHSHORT).show(); >

public void onServiceDisconnected(ComponentName className) { // unexpected!y disconnected,we should never see this happen.

boundService = null; Toast.makeText(TestServiceHolder.this, "Service disconnected ", Toast.LENGTHSHORT).show(); ) ) ;

private void initButtons() {

Button buttonStart = (Button) findViewBy!d(R.id.start Service); buttonStart.setOnClickList ener(new OnClickListener() { public void onClick(View argO) {

startService(); )

Button buttonStop = (Button) f indViewById(R.id.stop Service); butt onStop.setOnCllckListener(new OnClickListener () { public void onClick(View argO) { stopServi ce(); )

Button buttonBind = (Button) findViewByld(R.id.bind Service); buttonBmd. setOnClickListener (new OnClickListener() { public void onClick(View argO) (

I Button buttonUnbind = (Button) findViewById(R.id.unbind Service); buttonUnbind.setOnClickListener(new OnClickListener() ( public void onClick(View argO) { unbindService(); )

private void startService() {

Intent i = new Intent(this, TestService.class);

this.startService(1); }

private void stopService() {

Intent i = new intent(this, TestService.class); this.stopService(i); )

private void bindService() (

Intent 1 = new Intent(this, TestService.class); bindService(i, _connection, Context.BIND_AUTO_CREATE); isBound = true;

private void unbindService() {

if ( isBound) { unbindService( connection); isBound = false;

TestService!¡older M^Mfcl'Mfft Activity, onCreate «ff

%] main.xml, ji'Wt] setTitle "Service Test", fifcfiHffl (\SiXtfJ h'fk initButtons()i5

l^m'I' M/W^SiJWit^^HB^Wr^a-. start # bind, MtH&Mii intent HHjft<J.

ft:. intent Service stop tfe-ft , ftft&flfcft«» K0W:

private void startService() (

Intent i = new Intent(this, TestService.class); this.startService(i);

private void stopService() <

Intent i = new Intent (this, TestService.class); this . st.opService (i) ;

i&lit^h t ServiceConncction F:

private ServiceConnection connection = new ServiceConnection() \

public void onServiceConnected(ComponentName className, IBinder Service) boundService ((TestService.LocalBinder)Service).getService();

Toast.makeText(TestSorviceHolder.this, Toast.LENGTH SHORT).show();

'Service connected'

public void ohServiceDisconnected(ComponentName className) (

boundService = null; Toast.makeText(TestServiceHolder.this, "Service connected", Toast.LENGTH SHORT).show();

KflUIJ&tti Activity »«PfeM Service mm »IJMMMidr mmrnvrmmmimo

"Start Seme" mi »JVMMtom 10-4 MPS 10-4 Service iF.f/ I fH T. «cifc*

A® 10-3 iW®

* KM'. T..|ni/si</<Wr<W'»>kr>l/tlkUM. i.t. E<lii>i« n«M.

Ell« i«ii i»«rc. tilui.1 |>ri,.<. ;.r<k tr»;«c<

IT11MJCMI1I C»

««««I» P«i in «ntr.li »l»r»:t«rii «4r.it K«"ii Ml« «■ olr-,,1-1

aeoi

+1 0

Post a comment