Android混合方式开启服务
2017-02-18 15:42
127 查看
bind方式开启服务,可以调用服务的方法,但activity退出服务也随着退出,用 start 方式开启服务,可以保证activity关闭后,服务依然进行。很多需求都是两者的综合,比如音乐播放器,关闭activity,但音乐继续播放,activity还要调用Service方法,这就用到了混合方式。
①创建一个Service
public class MixService extends Service {
@Override
public IBinder onBind(Intent intent) {
System.out.println("onBind");
return new Binder();
}
@Override
public void onCreate() {
System.out.println("onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
System.out.println("onDestroy");
super.onDestroy();
}
}
②配置service
<service android:name="com.haowan.MixService"></service>③MainActivity中
public class MainActivity extends Activity {
private Intent service;
private MyConnection conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View v){
service = new Intent(getApplication(),MixService.class);
startService(service);
}
public void stop(View v){
stopService(service);
}
public void bindstart(View v){
conn = new MyConnection();
bindService(service, conn, BIND_AUTO_CREATE);
}
public void bindstop(View v){
unbindService(conn);
}
private class MyConnection implements ServiceConnection{
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("onServiceConnected");
}
public void onServiceDisconnected(ComponentName name) {
System.out.println("onServiceDisconnected");
}
}
}
基本原理就是 startService 与bindservice都要调用,退出页面只需要调用unbindService , 以满足bindService的要求,该服务还在后台运行,只要没有调stopService
实战:一个音乐播放器的框架(核心代码)
MainActivity
public class MainActivity extends Activity {
private MyBinder binder;
private ServiceConnection conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent service=new Intent(getApplicationContext(),MusicPlayerService.class);
conn = new MyConnection();
bindService(service, conn,BIND_AUTO_CREATE );
startService(service);
}
private class MyConnection implements ServiceConnection
{
public void onServiceConnected(ComponentName name, IBinder service) {
binder=(MyBinder) service;
}
public void onServiceDisconnected(ComponentName name) {
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
}
public void previous(View v){
binder.previous2();
}
public void play(View v){
binder.play2();
}
public void next(View v){
binder.next2();
}
}
Service 还要再清单文件中配置
public class MusicPlayerService extends Service{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new MyBinder();
}
public class MyBinder extends Binder{
public void next2(){
next();
}
public void play2(){
play();
}
public void previous2(){
previous();
}
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("准备一个音乐播放器");
}
public void next(){
System.out.println("播放下一首");
}
public void play(){
System.out.println("播放");
}
public void previous(){
System.out.println("播放上一首");
}
}
①创建一个Service
public class MixService extends Service {
@Override
public IBinder onBind(Intent intent) {
System.out.println("onBind");
return new Binder();
}
@Override
public void onCreate() {
System.out.println("onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
System.out.println("onDestroy");
super.onDestroy();
}
}
②配置service
<service android:name="com.haowan.MixService"></service>③MainActivity中
public class MainActivity extends Activity {
private Intent service;
private MyConnection conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View v){
service = new Intent(getApplication(),MixService.class);
startService(service);
}
public void stop(View v){
stopService(service);
}
public void bindstart(View v){
conn = new MyConnection();
bindService(service, conn, BIND_AUTO_CREATE);
}
public void bindstop(View v){
unbindService(conn);
}
private class MyConnection implements ServiceConnection{
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("onServiceConnected");
}
public void onServiceDisconnected(ComponentName name) {
System.out.println("onServiceDisconnected");
}
}
}
基本原理就是 startService 与bindservice都要调用,退出页面只需要调用unbindService , 以满足bindService的要求,该服务还在后台运行,只要没有调stopService
实战:一个音乐播放器的框架(核心代码)
MainActivity
public class MainActivity extends Activity {
private MyBinder binder;
private ServiceConnection conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent service=new Intent(getApplicationContext(),MusicPlayerService.class);
conn = new MyConnection();
bindService(service, conn,BIND_AUTO_CREATE );
startService(service);
}
private class MyConnection implements ServiceConnection
{
public void onServiceConnected(ComponentName name, IBinder service) {
binder=(MyBinder) service;
}
public void onServiceDisconnected(ComponentName name) {
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
}
public void previous(View v){
binder.previous2();
}
public void play(View v){
binder.play2();
}
public void next(View v){
binder.next2();
}
}
Service 还要再清单文件中配置
public class MusicPlayerService extends Service{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new MyBinder();
}
public class MyBinder extends Binder{
public void next2(){
next();
}
public void play2(){
play();
}
public void previous2(){
previous();
}
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("准备一个音乐播放器");
}
public void next(){
System.out.println("播放下一首");
}
public void play(){
System.out.println("播放");
}
public void previous(){
System.out.println("播放上一首");
}
}
相关文章推荐
- Android(java)学习笔记231:服务(service)之混合方式开启服务
- Android - 绑定服务调用服务里面的方法,start/bind开启服务的比较,混合方式开启服务,本地服务,远程服务,系统服务,录音API
- [android] 绑定方式开启服务&调用服务的方法
- 使用混合方式开启服务----百度音乐盒框架的搭建
- Android中服务的开启和停止的方式以及各种情况下的存活状态
- 混合的方式开启服务
- Android服务Service详解(作用,生命周期,AIDL)系列文章--服务绑定和开启混合使用
- Android中服务(Service)的的生命周期,两种开启服务的方式的不同点,Service与Activity之间的通讯
- Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务
- 混合的方式开启服务
- android中服务的开启方式及他们的区别和联系
- 混合方式开启服务的特点
- [android] 服务的生命周期(混合方式)
- 混合的方式开启服务
- Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务
- win7快捷方式开启ORACLE服务的方法
- 用命令行方式开启和关闭oracle服务
- Android不同层次开启硬件加速的方式
- Android不同层次开启硬件加速的方式
- Android窗口管理服务WindowManagerService对窗口的组织方式分析