易网时代-编程资源站
Welcome
微信登录
首页
/
操作系统
/
Linux
/
Android开发教程:Service简析
简介
Service是Android 四大组件之一,它跟Activity的级别差不多,但是他不能自己运行,只能后台运行,并且可以和其他组件进行交互。Service的启动有两种方式:context.startService()和context.bindService()。
1.使用context.startService()启动Service
生命周期:
context.startService() ->onCreate()- >onStart()->Servicerunning->context.stopService()
onDestroy() ->Service stop
如果Service还没有运行,则android先调用onCreate()然后调用onStart();如果Service已经运行,则只调用onStart(),所以一个Service的onStart方法可能会重复调用多次。
stopService的时候直接onDestroy,如果是调用者自己直接退出而没有调用stopService的话,Service会一直在后台运行。该Service的调用者再启动起来后可以通过stopService关闭Service。
所以调用startService的生命周期为:onCreate --> onStart(可多次调用) --> onDestroy
2.使用context.bindService()启动Service
生命周期:
context.bindService()->onCreate()->onBind()->Service runningonUnbind() ->onDestroy() ->ServicestoponBind将返回给客户端一个IBind接口实例,IBind允许客户端回调服务的方法,比如得到Service运行的状态或其他操作。这个时候把调用者(Context,例如Activity)会和Service绑定在一起,Context退出了,Srevice就会调用onUnbind->onDestroy相应退出。
所以调用bindService的生命周期为:onCreate --> onBind(只一次,不可多次绑定) --> onUnbind -->onDestory。
在Service每一次的开启关闭过程中,只有onStart可被多次调用(通过多次startService调用),其他onCreate,onBind,onUnbind,onDestory在一个生命周期中只能被调用一次。
service可以在和多场合的应用中使用,比如播放多媒体的时候用户启动了其他Activity这个时候程序要在后台继续播放,比如检测SD卡上文件的变化,再或者在后台记录你地理信息位置的改变等等。下面是一个实际的例子:这个例子有四个类:
其中和Service有关的是PlayMusicActivit.java和MusicService.javaPlayMusicActivit是一个启动界面上面有四个按钮分别来启动、暂停、停止和关闭Service
MusicService是一个实际的Service类另外连个类是用来做通知的,将在通知里面讲解
package
com.my;
import
android.app.Activity;
import
android.app.NotificationManager;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
public
class
PlayMusicActivity
extends
Activity {
private
static
final
int
NOTIFICATION_ID =
10001
;
private
Button playBtn;
private
Button stopBtn;
private
Button pauseBtn;
private
Button closeBtn;
private
Button exitBtn;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
playBtn = (Button) findViewById(R.id.play_btn);
stopBtn = (Button) findViewById(R.id.stop_btn);
pauseBtn = (Button) findViewById(R.id.pause_btn);
closeBtn = (Button) findViewById(R.id.close_btn);
exitBtn = (Button) findViewById(R.id.exit_btn);
playBtn.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
Intent service =
new
Intent(
"com.my.musicService"
);
Bundle value =
new
Bundle();
value.putInt(
"opt"
,
1
);
service.putExtras(value);
startService(service);
}
});
stopBtn.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
Intent service =
new
Intent(
"com.my.musicService"
);
Bundle value =
new
Bundle();
value.putInt(
"opt"
,
2
);
service.putExtras(value);
startService(service);
}
});
pauseBtn.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
Intent service =
new
Intent(
"com.my.musicService"
);
Bundle value =
new
Bundle();
value.putInt(
"opt"
,
3
);
service.putExtras(value);
startService(service);
}
});
closeBtn.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
PlayMusicActivity.
this
.finish();
}
});
exitBtn.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
Intent service =
new
Intent(
"com.my.musicService"
);
stopService(service);
final
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(NOTIFICATION_ID);
PlayMusicActivity.
this
.finish();
}
});
}
}
package
com.my;
import
java.io.IOException;
import
android.app.Service;
import
android.content.Intent;
import
android.media.MediaPlayer;
import
android.os.Bundle;
import
android.os.IBinder;
import
android.util.Log;
public
class
MusicService
extends
Service {
private
static
final
String TAG =
"MusicService"
;
private
MediaPlayer mediaPlayer;
@Override
public
IBinder onBind(Intent intent) {
return
null
;
}
@Override
public
void
onCreate() {
Log.i(TAG,
"create service"
);
super
.onCreate();
if
(mediaPlayer ==
null
) {
mediaPlayer = MediaPlayer.create(
this
, R.raw.he);
mediaPlayer.setLooping(
false
);
}
}
@Override
public
void
onDestroy() {
Log.i(TAG,
"destroy service"
);
super
.onDestroy();
if
(mediaPlayer !=
null
) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
@Override
public
void
onStart(Intent intent,
int
startId) {
Log.i(TAG,
"start service"
);
super
.onStart(intent, startId);
if
(intent !=
null
) {
Bundle bundle = intent.getExtras();
if
(bundle !=
null
) {
int
opt = bundle.getInt(
"opt"
);
switch
(opt) {
case
1
:
play();
break
;
case
2
:
stop();
break
;
case
3
:
pause();
break
;
}
}
}
}
private
void
play() {
if
(!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
private
void
pause() {
if
(mediaPlayer !=
null
&& mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
private
void
stop() {
if
(mediaPlayer !=
null
) {
mediaPlayer.stop();
try
{
mediaPlayer.prepare();
}
catch
(IllegalStateException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
}
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图