http://express.ruanko.com/ruanko-express_22/webpage/tech-overnight_1.html
Android中的Service,其意思是“服务”,它是在后台运行,不可交互的。Service自己不能运行,需要通过某一个Activity或者其它Context对象来调用,如Context.startService()和Context.bindService()两种方式启动Service。
如果在Service的onCreate或者onStart方法中做一些很耗时的动作,最好是启动一个新线程来运行这个Service,因为,如果Service运行在主线程中,会影响到程序的UI操作或者阻塞主线程中的其它事情。
一个使用Service的典型的例子是,用户一边在手机做其他的事情,一边听手机上的音乐。其中,在后台播放音乐就可以使用Service来实现。在这个应用中,音乐播放这个功能并没有对应的Activity,因为使用者会认为在导航到其他屏幕时,音乐应该还在播放。
Service的生命周期方法比Activity要少一些,只有onCreate、onStart、onDestroy。有两种方式启动一个Service,他们对Service生命周期的影响是不一样的。
1)通过startService启动
Service启动的时候会经历生成 开始(onCreateonStart)过程,Service停止的时候直接进入销毁过程(onDestroy)。而如果是调用者直接退出而没有调用stopService,Service会一直在后台运行。直到下次调用者再启动起来,并明确调用stopService。
2)通过bindServic启动
通过bindService方法启动Service,其只会运行onCreate方法,如果调用这退出了,Service会调用onUnbind onDestroyed方法。
Service的onCreate方法只会被调用一次。如果先绑定了,那么启动的时候就直接运行Service的onStart方法,如果先启动,那么绑定的时候就直接运行onBind方法。如果先绑定上了,就停止不了,也就是stopService不能用了,只能先unbindService,再stopService,所以,先启动还是先绑定,是有区别的。
下面以一个通过Service来播放音乐的例子说明Service的具体用法,其具体界面如下所示:
在这个界面中,点击“Start Playing”按钮,即开始打开音乐文件,进行循环播放。点击“Stop Playing”按钮,即关闭音乐,并退出应用程序。其程序结构如下图所示:
可以看到,其功能主要由Music与TestMusicService两个类组成,其源代码如下:
package com.shen.service;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class TestMusicService extends Activity {
private TextView tv;
private Button btn1,btn2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) this.findViewById(R.id.btn1);
btn1.setOnClickListener(new ButtonListener1());
btn2 = (Button) this.findViewById(R.id.btn2);
btn2.setOnClickListener(new ButtonListener2());
}
private class ButtonListener1 implements OnClickListener{
public void onClick(View v) {
TestMusicService.this.startService(new Intent("com.shen.music1"));
}
}
private class ButtonListener2 implements OnClickListener{
public void onClick(View v) {
TestMusicService.this.stopService(new Intent("com.shen.music1"));
finish();
}
}
}
package com.shen.service;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class Music extends Service {
private MediaPlayer mp;
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onStart (Intent intent, int startId) {
super.onStart(intent, startId);
if (mp != null){
mp.stop();
}
mp = MediaPlayer.create(this,R.raw.music_1);
mp.setLooping(true);
mp.start();
}
public void onDestroy () {
super.onDestroy();
mp.stop();
mp = null;
}
}
另外,需要在Manifest.xml文件中对Service进行注册。其注册信息如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shen.service"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestMusicService"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".Music">
<intent-filter>
<action android:name="com.shen.music1"/>
<category android:name="android.intent.category.default"/>
</intent-filter>
</service>
</application>
<uses-sdk android:minSdkVersion="2" />
</manifest>