码迷,mamicode.com
首页 > 移动开发 > 详细

(五)认识Android中的Service

时间:2016-01-07 20:04:59      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

一、使用Service

1.右击java文件夹,选择新建Service,然后重写其中的onStartCommand函数,只要执行了startService函数,onStartCommand便会被执行

package com.example.shiyanshi.learnservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class MyService extends Service {
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
//throw new UnsupportedOperationException("Not yet implemented");
return new Binder(); //要被重写,否则会弹出上面的错误
}


//执行startService时被调用
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("Service onStartCommand");

new Thread(new Runnable(){
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("******************Service is running********************");
}
}

}).start();
return super.onStartCommand(intent, flags, startId);
}

//    @Override
// public void onCreate() {
// super.onCreate();
// System.out.println("**************Service onCreate*****************");
// }
//
// @Override
// public void onDestroy() {
// super.onDestroy();
// System.out.println("****************Service onDestroy***************");
// }
}

2.启动service时调用startService函数,关闭service时调用stopService函数,该函数需要一个Intent,无论是像下述方式创建intent还是通过

startService(new Intent(MainActivity.this,MyService.class));stopService(new Intent(MainActivity.this,MyService.class));打开或者关闭的服务均是同一个


public class MainActivity extends Activity implements View.OnClickListener, ServiceConnection {
private Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

intent=new Intent(MainActivity.this,MyService.class);

findViewById(R.id.btnStartService).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startService(intent);
}
});

findViewById(R.id.btnStopService).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopService(intent);
}
});
 

二、绑定Service

1.绑定Service时调用bindService函数,public abstract boolean bindService (Intent service, ServiceConnection conn, int flags)  其第二个参数表示服务的连接,服务的连接主要用来侦听服务的状态,可以传递this指针(当传递this时该服务便与当前Activity绑定在一起了,当退出当前Activity时,服务便会自动被系统关闭(调用onDestroy函数,并且会弹出一个错误信息)),第三个参数可以为BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_NOT_FOREGROUND, BIND_ABOVE_CLIENT, BIND_ALLOW_OOM_MANAGEMENT, or BIND_WAIVE_PRIORITY.注意当第二个参数传递this时,还要在当前Activity类中实现onServiceConnected和onServiceDisconnected,否则会出现类型不匹配的错误,其中onServiceConnected函数在绑定服务成功后执行,onServiceDisconnected服务所在的进程崩溃或被杀掉的时候被执行。

public class MainActivity extends Activity implements View.OnClickListener, ServiceConnection {
private Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

intent=new Intent(MainActivity.this,MyService.class);

findViewById(R.id.btnStartService).setOnClickListener(this);
findViewById(R.id.btnStopService).setOnClickListener(this);
findViewById(R.id.btnBindService).setOnClickListener(this);
findViewById(R.id.btnUnbindService).setOnClickListener(this);

}


@Override
public void onClick(View view) {
switch (view.getId())
{
case R.id.btnStartService:
startService(intent);
break;
case R.id.btnStopService:
stopService(intent);
break;
case R.id.btnBindService:
bindService(intent, this, Context.BIND_AUTO_CREATE);
break;
case R.id.btnUnbindService:
unbindService(this);
break;
default:
break;
}

}

//服务被绑定成功成功后执行
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
System.out.println("***************Service Connect Successful********************");
}

//服务所在的进程崩溃或被杀掉的时候被执行
@Override
public void onServiceDisconnected(ComponentName componentName) {
System.out.println("***************Service disconnect Successful********************");
}
}
 
public class MyService extends Service {
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
return new Binder();
}


//执行startService时被调用
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("Service onStartCommand");
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onCreate() {
super.onCreate();
System.out.println("**************Service onCreate*****************");
}

@Override
public void onDestroy() {
super.onDestroy();
System.out.println("****************Service onDestroy***************");
}
}

2.Service程序

public class MyService extends Service {
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
return new Binder();
}


//执行startService时被调用
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("Service onStartCommand");
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onCreate() {
super.onCreate();
System.out.println("**************Service onCreate*****************");
}

@Override
public void onDestroy() {
super.onDestroy();
System.out.println("****************Service onDestroy***************");
}
}

三、Service生命周期

1.onCreate启动服务或者绑定服务时执行,注意若启动了服务的同时,又绑定了服务,该函数只会执行一次(最先启动或绑定的那次被执行)

2.onDestroy解除绑定或者停止服务时被执行,注意若启动了服务的同时,又绑定了服务,该函数只会在服务被停止并且绑定解除后执行(最后停止服务或解除绑定的那次被执行)

3.onStartCommand可以多次执行,只要执行了startService函数,该函数就会被执行;注意onCreate函数只有第一次执行startService函数(或bindService函数)时才会被执行,即只能执行一次。

4.onServiceConnected只在服务绑定成功后执行,注意只能执行一次。

5.onServiceDisconnected服务所在的进程崩溃或被杀掉的时候被执行。

(五)认识Android中的Service

标签:

原文地址:http://www.cnblogs.com/ql698214/p/5110974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!