Service的基本概念呢,在这里就不阐述了。我们直接说,Service是如何使用的吧。
我们以一个音乐播放的例子讲述:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="startMyService"
android:text="启动服务" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="stopMyService"
android:text="停止服务" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="bindMyService"
android:text="绑定服务" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="unBindMyService"
android:text="解绑服务" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="playMusic"
android:text="播放" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="pauseMusic"
android:text="暂停" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="continueMusic"
android:text="继续" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="stopMusic"
android:text="停止" />
</LinearLayout>
MainActivity.java
public class MyServiceActivity extends Activity {
private ServiceConnection conn ;
private Intent mService;
private MyDefineService mdService;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mService = new Intent(this,MyDefineService.class);
conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyDefineService.MyBindService s = (MyDefineService.MyBindService)service;
mdService = s.getService();
Log.i("INFO","mdService============="+mdService);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
}
public void startMyService(View v){
this.startService(mService);//intent激活组件
}
public void stopMyService(View v){
this.stopService(mService);
}
public void bindMyService(View v){
this.bindService(mService, conn, Context.BIND_AUTO_CREATE);
}
public void unBindMyService(View v){
this.unbindService(conn);
}
public void playMusic(View v){
mdService.playMusic();
}
public void pauseMusic(View v){
mdService.pauseMusic();
}
public void continueMusic(View v){
mdService.continueMusic();
}
public void stopMusic(View v){
mdService.stopMusic();
}
}
MyDefineService:
public class MyDefineService extends Service {
private MediaPlayer player = new MediaPlayer();
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i("INFO","启动服务。。。");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("INFO","销毁服务。。。");
}
@Override
public IBinder onBind(Intent intent) {
Log.i("INFO","绑定onBind");
return new MyBindService();
}
class MyBindService extends Binder{
public MyDefineService getService(){
return MyDefineService.this;
}
}
@Override
public boolean onUnbind(Intent intent) {
Log.i("INFO","解除绑定onUnbind");
return super.onUnbind(intent);
}
public void playMusic(){
try {
player.reset();
player.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath()+"/baorong.mp3");
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void pauseMusic(){
player.pause();
}
public void continueMusic(){
player.start();
}
public void stopMusic(){
player.stop();
}
}