简介:Android中的服务和windows中的服务是类似的东西,服务一般没有用户操作界面,它运行于系统中不容易被用户发觉,可以使用它开发如监控或下载之类的程序。服务的开发比较简单,如下:
第一步:继承Service类
public class SMSService extends Service { }
第二步:在AndroidManifest.xml文件中的<application>节点里对服务进行配置:
<service android:name=".SMSService" />
服务不能自己运行,需要通过调用Context.startService()或Context.bindService()方法启动服务。这两个方法都可以启动Service,但是它们的使用场合有所不同。使用startService()方法启用服务,访问者与服务之间没有关连,即使访问者退出了,服务仍然运行。使用bindService()方法启用服务,访问者与服务绑定在了一起,访问者一旦退出,服务也就终止,大有“不求同时生,必须同时死”的特点。
当采用Context.startService()方法启动服务,与之有关的生命周期方法
onCreate()-- onStart() --onDestroy()
activitiy可通过startService方法和stopService方法控制服务的开启与关闭
如果一个Service被startService方法多次启动,那么onCreate方法只会调用一次,并且系统只会创建Service的一个实例(因此你应该知道只需要一次stopService调用)如果一个Service被startService方法多次启动,那么onCreate方法只会调用一次,onStartCommand将会被调用多次(对应调用startService的次数)
通过startService方式启动服务 即使调用者退出 服务依然在后台运行除非让另外的组件调用onDestroy(杀死服务)或是手动杀死
案例
1编写布局文件 设置俩个按钮点击事件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="start"
android:text="startservice" />
<Button
android:id="@+id/button2"
android:onClick="stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="StopService" />
</RelativeLayout>
2在应用程序界面具体编写点击事件
public class MainActivity extends Activity {
Intent intent=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//若想调用MainActivity.this必须在setContentView之后调用
intent= new Intent(MainActivity.this,MyService.class);
}
public void start(View view) {
//启动service
startService(intent);// onCreate onStartCommand onDestroy
}
public void stop(View view) {
//结束service
stopService(intent);
}
}
3编写自己的服务 重写 onCreate()-- onStart() --onDestroy()方法
public class MyService extends Service {
private static final String TAG = "chj";
MediaPlayer mediaPlayer=null;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
// onCreate onStartCommand onDestroy
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.e(TAG, "onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
//每次启动服务就会调用一次
Log.e(TAG, "onStartCommand");
if (mediaPlayer==null) {
mediaPlayer=MediaPlayer.create(MyService.this,R.raw.someone_like_you);
}
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
//让另外的组件调用onDestroy -->杀死进程
//通过startService方式启动服务 即使调用者退出 服务依然在后台运行
Log.e(TAG, "onDestroy");
// TODO Auto-generated method stub
super.onDestroy();
if (mediaPlayer!=null) {
mediaPlayer.stop();
}
}
}
4在清单文件配置service
<service android:name=".MyService"></service>
补充:要在res下新建一个raw文件夹并放入名称为someone_like_you.mp3文件
startService启动服务的生命周期中有个start()和startCommond()方法,具体前者已经废弃,可能功能一样,本人也不是很清楚
另外:startService可以多次调用,此时service的onStartCommand也会多次调用,stopService也可以多次调用,但实际上Service的onDestory只会调用一次。注意下一节内容的对比