在工程里想要添加一个Service,先新建继承Service的类,然后到 AndroidManifest.xml -> Application ->Application Nodes中的Service标签中添加:
一、添加Service声明
<service android:name=".CounterService" android:enabled="true">
</service>
二、两种方式启动service
Service要由Activity通过startService 或者 bindService来启动,Intent负责传递参数。
startService与bindService都可以启动Service,它们两者的区别就是使Service的周期改变。
(1) startService启动的Service必须要有stopService来结束Service,不调用stopService则会造成Activity结束了而Service还运行着。
(2) bindService启动的Service可以由unbindService来结束,也可以在 Activity结束之后(onDestroy)自动结束。
1、Activity通过startService
参考:http://android.tgbus.com/Android/tutorial/201107/359322.shtml
启动一个Service的过程如下:context.startService() ->onCreate()- >onStart()->Service running其中onCreate()可以进行一些服务的初始化工作,onStart()则启动服务。
停止一个Service的过程如下:context.stopService() | ->onDestroy() ->Service stop
接下来的实例是一个利用后台服务播放音乐的小例子,点击start运行服务,点击stop停止服务。
1.1 创建Service类
package test.cbl.srv;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service created", Toast.LENGTH_LONG).show();
Log.i(TAG, "onCreate");
player = MediaPlayer.create(this,R.raw.mine);
player.setLooping(false);
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stoped", Toast.LENGTH_LONG).show();
Log.i(TAG, "onDestroy");
player.stop();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Start", Toast.LENGTH_LONG).show();
Log.i(TAG, "onStart");
player.start();
}
}
1.2 Activity中启动Service
package test.cbl.srv;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ServiceDemo extends Activity implements OnClickListener {
private static final String TAG = "ServiceDemo";
Button buttonStart, buttonStop;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}
public void onClick(View src){
switch (src.getId()) {
case R.id.buttonStart:
Log.i(TAG, "onClick: starting service");
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
Log.i(TAG, "onClick: stopping service");
stopService(new Intent(this, MyService.class));
break;
}
}
}
1.3 string.xml字符串定义
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">播放器服务</string>
<string name="start">Start</string>
<string name="stop">Stop</string>
<string name="services_demo">Service Demo</string>
</resources>
1.4 main.xml布局文件layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/services_demo" android:gravity="center" android:textSize="20sp" android:padding="20dp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonStart" android:text="@string/start"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop" android:id="@+id/buttonStop"></Button>
</LinearLayout>
1.5 Manifest.xml 添加Service声明
<service android:enabled="true" android:name=".MyService"/>
2、bindService方式启动Service
2.1 Activity中Oncreate中:
//建立和service的通讯
Intent bindIntent = new Intent(MainActivity.this, CounterService.class);
bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE);
2.2 类中创建connection对象
private ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
counterService = ((CounterService.CounterBinder)service).getService();
Log.i(LOG_TAG, "Counter Service Connected");
}
public void onServiceDisconnected(ComponentName className) {
counterService = null;
Log.i(LOG_TAG, "Counter Service Disconnected");
}
};