WEB开发网
开发学院手机开发Android 开发 Android 开发教程之 Service 详解 阅读

Android 开发教程之 Service 详解

 2010-02-22 16:14:00 来源:WEB开发网   
核心提示:public void onClick(View arg0) {unbindService;}});}private void startService {Intent i = new Intent(this, TestService.class);this.startService(i);}private void

public void onClick(View arg0) {

unbindService;

}

});

}

private void startService {

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

this.startService(i);

}

private void stopService {

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

this.stopService(i);

}

private void bindService {

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

bindService(i, _connection, Context.BIND_AUTO_CREATE);

_isBound = true;

}

private void unbindService {

if (_isBound) {

unbindService(_connection);

_isBound = false;

}

}

}

这里可以看到两种启动方法,start和bind,当然也是通过intent调用的,在intent中指明指定要启动的Service的名字,stop也一样 :

Java代码 复制代码

1. private void startService {

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

3. this.startService(i);

4. }

5.

6. private void stopService {

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

8. this.stopService(i);

9. }

private void startService {

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

this.startService(i);

}

private void stopService {

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

this.stopService(i);

}

对于bind的话,需要一个ServiceConnection对象

Java代码 复制代码

1. private ServiceConnection _connection = new ServiceConnection {

2. public void onServiceConnected(ComponentName className, IBinder service) {

3. _boundService = ((TestService.LocalBinder)service).getService;

4.

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

上一页  3 4 5 6 7 8 9 10  下一页

Tags:Android 开发 教程

编辑录入:coldstar [复制链接] [打 印]
赞助商链接