一:service的分类
1.startService
使用:调用本地服务。
生命周期:oncreate--onstartcommand--service running--ondestroy
若此service未运行,调用时运行--context.startservice--on create--on startconmand--service running;
若此service已经被运行过,调用时运行--on startcommand--service running;
关闭时调用:context.stopservice--ondestroy--service stop;注:若界面退出时未调用stopservice,则此服务会一直存在与设备中,直至内存不足时被强制关闭。
2.bindService
使用:调用远程服务。
生命周期:oncreate--onbind--onbind--ondestroy;注:此service只可以被绑定一次,当调用者退出时,service会调用onunbind--ondestroy方法关闭服务。
3.AIDL
使用:实现进程之间的通信
二:简单实例
1.startservice;
实现功能:实现简单的音乐播放
步骤:创建类继承service,实现其onCreate(),和 onStart()方法。通过intent进行参数的传递。
package com.service.activity;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class service1 extends Service{
MediaPlayer mediaplayer;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
mediaplayer=MediaPlayer.create(getApplicationContext(), R.raw.fh);
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
switch (intent.getExtras().getInt("key")) {
case 1:
if(mediaplayer.isPlaying()!=true){
mediaplayer.start();
mediaplayer.setLooping(false);
}
break;
case 2:
if(mediaplayer.isPlaying()==true&&mediaplayer!=null){
mediaplayer.pause();
}
break;
case 3:
if(mediaplayer!=null){
mediaplayer.stop();;
}
break;
default:
break;
}
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
}
MainActivity类:
package com.service.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
Button play;
Button pause;
Button Stop;
Button exit;
Button exit_stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
play.setOnClickListener(this);
pause.setOnClickListener(this);
Stop.setOnClickListener(this);
exit.setOnClickListener(this);
exit_stop.setOnClickListener(this);
}
public void init(){
play=(Button) findViewById(R.id.play);
pause=(Button) findViewById(R.id.pause);
Stop=(Button) findViewById(R.id.Stop);
exit=(Button) findViewById(R.id.exit);
exit_stop=(Button) findViewById(R.id.exit_stop);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.play:
start(1);
break;
case R.id.pause:
pause(2);
break;
case R.id.Stop:
stop(2);
break;
case R.id.exit:
finish();
break;
case R.id.exit_stop:
Intent intent3=new Intent(getApplicationContext(), service1.class);
stopService(intent3);
finish();
break;
default:
break;
}
}
public void start(int i){
Intent intent=new Intent(getApplicationContext(), service1.class);
Bundle b=new Bundle();
b.putInt("key", 1);
intent.putExtras(b);
startService(intent);
}
public void pause(int i){
Intent intent1=new Intent(getApplicationContext(), service1.class);
Bundle b1=new Bundle();
b1.putInt("key", 2);
intent1.putExtras(b1);
startService(intent1);
}
public void stop(int i){
Intent intent2=new Intent(getApplicationContext(), service1.class);
Bundle b2=new Bundle();
b2.putInt("key", 3);
intent2.putExtras(b2);
startService(intent2);
}
}
XML文件的配置;
<service android:name="com.service.activity.service1"></service>
2.bindservice
实现功能:实现音乐播放,区别:不需要每次点击都与service通信,只需建立一次链接,而后只需调用service的方法即可。
代码如下:
package com.service_bind.activity;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
public class service_bind extends Service{
final IBinder bin=new mybind();
MediaPlayer mediaplayer;
final static String s="com.service_bind.bindserver";
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return bin;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
if(mediaplayer!=null){
mediaplayer.stop();
mediaplayer.release();
}
super.onDestroy();
}
public class mybind extends Binder{
public service_bind getservice(){
return service_bind.this;
}
}
public void play(){
if (mediaplayer == null) {
mediaplayer = MediaPlayer.create(this, R.raw.fh);
mediaplayer.setLooping(false);
}
if (!mediaplayer.isPlaying()) {
mediaplayer.start();
}
}
public void pause(){
if (mediaplayer.isPlaying()) {
mediaplayer.pause();
}
}
public void stop(){
mediaplayer.stop();
}
}
MainActivity:
package com.service_bind.activity;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
Button play;
Button pause;
Button Stop;
Button exit;
Button exit_stop;
service_bind service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
play.setOnClickListener(this);
pause.setOnClickListener(this);
Stop.setOnClickListener(this);
creatcon();
}
public void init(){
play=(Button) findViewById(R.id.play);
pause=(Button) findViewById(R.id.pause);
Stop=(Button) findViewById(R.id.Stop);
exit=(Button) findViewById(R.id.exit);
exit_stop=(Button) findViewById(R.id.exit_stop);
}
public void creatcon(){
Intent inten=new Intent(service_bind.s);
bindService(inten, connext, 1);
}
ServiceConnection connext=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
service=null;
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
service=((service_bind.mybind)arg1).getservice();
service.play();
}
};
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
if(connext!=null){
unbindService(connext);
}
super.onDestroy();
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.play:
service.play();
break;
case R.id.pause:
service.pause();
break;
case R.id.Stop:
service.stop();
break;
default:
break;
}
}
}
XML文件的配置:
<service android:name="com.service_bind.activity.service_bind"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.service_bind.bindserver"/>
</intent-filter>
</service>