android中的Service(服务)是一个什么样的东东呢?如果你对Windows系统中的服务理解,可以认为他们同理。如果你不了解也没有关系,你只管把Service当成一个没有界面的Activity就可以了。
Service是运行在后台,是不可见的、没有界面的东西。你可以启动一个服务Service来播放音乐,或者记录你地理信息位置的改变,或者启动一个服务来运行并一直监听电话、短信等操作。
既然我们把Service当成一个无界的Activity来看待,那么它也是运行在主线程,因而不能用它来做耗时的请求或者动作。如果有耗时的操作,那么同样需要在服务中开一个线程,在线程中做耗时操作。
1、服务一般分为两种:
(1)本地服务, Local Service 用于应用程序内部。在Service可以调用Context.startService()启动,调用Context.stopService()结束。在内部可以调用Service.stopSelf() 或 Service.stopSelfResult()来自己停止。无论调用了多少次startService(),都只需调用一次stopService()来停止。
(2)远程服务, Remote Service 用于android系统内部的应用程序之间。可以定义接口并把接口暴露出来,以便其他应用进行操作。客户端建立到服务对象的连接,并通过那个连接来调用服务。调用Context.bindService()方法建立连接,并启动,以调用 Context.unbindService()关闭连接。多个客户端可以绑定至同一个服务。如果服务此时还没有加载,bindService()会先加载它。
提供给可被其他应用复用,比如定义一个天气预报服务,提供与其他应用调用即可。
2、Service生命周期:
Service的生命周期比Activity要简单一些,仅继承了onCreate()、onStart()、onDestroy()三个方法。
当我们第一次启动Service时,先后调用了onCreate()、onStart()这两个方法;当停止Service时,则执行onDestroy()方法。
如果Service已经启动了,当我们再次启动Service时,不会在执行onCreate()方法,而是直接执行onStart()方法。它可以通过Service.stopSelf()方法或者Service.stopSelfResult()方法来停止自己,只要调用一次stopService()方法便可以停止服务,无论调用了多少次的启动服务方法。
本章案例为Activity与Service绑定用法。
一、设计界面
1、布局文件
打开res/layout/activity_main.xml文件。
输入以下代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/startservice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动Service" />
<Button
android:id="@+id/stopservice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止Service" />
<Button
android:id="@+id/bindservice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定Service" />
<Button
android:id="@+id/unbindservice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解除Service" />
</LinearLayout>
二、程序文件
1、打开“src/com.genwoxue.service/ServiceUtil.java”文件。
然后输入以下代码:
package com.genwoxue.servicebind;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class ServiceUtil extends Service {
private String TAG="Service:";
private IBinder binder=new Binder(){
@Override
public String getInterfaceDescriptor(){
return "Service class";
}
};
@Override
public IBinder onBind(Intent intent){
Log.i(TAG, "服务开始绑定:onBind() Intent="+intent);
return binder;
}
@Override
public void onRebind(Intent intent){
Log.i(TAG, "服务重新绑定:onRebind() Intent="+intent);
super.onRebind(intent);
}
@Override
public boolean onUnbind(Intent intent){
Log.i(TAG, "服务解除绑定:onUnbind() Intent="+intent);
return super.onUnbind(intent);
}
@Override
public void onCreate(){
Log.i(TAG, "服务开始创建:onCreate()!");
}
@Override
public void onDestroy(){
Log.i(TAG, "服务销毁:onDestroy()!");
}
@Override
public int onStartCommand(Intent intent,int flags,int startId){
Log.i(TAG, "服务启动:onStart()=>Intent"+intent+",startID="+startId);
return Service.START_CONTINUATION_MASK;
}
2、打开“src/com.genwoxue.service/MainActivity.java”文件。
输入以下代码:
package com.genwoxue.servicebind;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.ComponentName;
public class MainActivity extends Activity {
private Button btnStart=null;
private Button btnStop=null;
private Button btnBind=null;
private Button btnUnBind=null;
private String TAG="Service:";
private ServiceConnection serviceConn=new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name,IBinder service){
try{
Log.i(TAG,"服务连接成功:service="+service.getInterfaceDescriptor());
}catch(RemoteException e){
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name){
Log.i(TAG,"服务断开连接!");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.print("hello");
btnStart=(Button)super.findViewById(R.id.startservice);
btnStop=(Button)super.findViewById(R.id.stopservice);
btnBind=(Button)super.findViewById(R.id.bindservice);
btnUnBind=(Button)super.findViewById(R.id.unbindservice);
btnStart.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
Intent intent=new Intent(MainActivity.this,ServiceUtil.class);
MainActivity.this.startService(intent);
}
});
btnStop.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
Intent intent=new Intent(MainActivity.this,ServiceUtil.class);
MainActivity.this.stopService(intent);
}
});
btnBind.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
Intent service=new Intent(MainActivity.this,ServiceUtil.class);
MainActivity.this.bindService(service,MainActivity.this.serviceConn,Context.BIND_AUTO_CREATE);
}
});
btnUnBind.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
MainActivity.this.unbindService(MainActivity.this.serviceConn);
}
});
}
}
三、配置文件
打开“AndroidManifest.xml”文件。
然后输入以下代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.genwoxue.servicebind"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.genwoxue.servicebind.MainActivity"
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="com.genwoxue.servicebind.ServiceUtil" />
</application>
</manifest>
注意:由于我们要启用服务,需要在AndroidManifest.xml文件中添加以下内容:
<service android:name="com.genwoxue.servicebind.ServiceUtil" />
四、运行结果
1、第一步、在LogCat中创建“Service”过滤器
2、运行Service App
参考推荐:
Android BroadcastReceiver启动Service