Android中的Service,AndroidService
Android 中的 Service
Service简介:
Service是被设计用来在后台执行一些需要长时间运行的操作。
Android由于允许Service在后台运行,甚至在结束Activity后,因此相对来说,Service相比Activity拥有更高的优先级。
创建Service:
要创建一个最基本的Service,需要完成以下工作:1)创建一个Java类,并让其继承Service 2)重写onCreate()和onBind()方法
其中,onCreate()方法是当该Service被创建时执行的方法,onBind()是该Service被绑定时执行的方法。
public class ExampleService extends Service{ @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); } }
当创建了一个新的Service后,还必须在AndroidManifest.xml文件中对他进行配置,需要在application节点内包含一个Service标记。
<service android:name=".ExampleService" android:enabled="true" android:permission="exam02.chenqian.com.servicedemo"></service>
当然,如果你想要你自定义的Service仅能被自己编写的该应用程序使用,还可以在标签内添加:android:permission="exam02.chenqian.com.servicedemo"
让Service执行特定的任务:
如果想要Service执行特定的任务,可以复写Service的onStartCommand()方法,注意在API15之前为onStart()方法,现已不推荐,onStartCommand()方法的执行为该Service onCreate()之后。
@Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); }
启动和停止Service:
显式启动一个Service:
// 显示启动ExampleService Intent intent = new Intent(this,ExampleService.class); // 启动ExampleService startService(intent);
为了方便观察,我们可以在之前创建的自定义的Service类中的onStartCommand()方法中添加Log.i("ServiceState","-------------->is Running");
当我们从MainActivity调用运行时,可以在Logcat中观察到输出: I/ServiceState: is Running
当然,我们也可以停止一个Service,为了让我们更清晰的观察到效果,我们可以在ExampleService类中复写onDestroy()方法:
@Override public void onDestroy() { Log.i("ServiceState","------------------>Destroy"); super.onDestroy(); }
可以在MainActivity中通过以下方式停止一个Service:
显示停止一个Service:注意,写这里时更换了一个Service,并将该自定义的Service定位MyService,已经不是之前的ExampleService,不过您认可按照自己之前的继续编写,毕竟方法都是一样的;-)
//显示关闭Service Intent serviceIntent = new Intent(MainActivity.this,MyService.class); //关闭Service stopService(serviceIntent);
注意Service的调用不可嵌套,因此无论Service被调用了多少次,对stopService()停止的一次调用就会终止它所匹配运行中的Service。
由于Service具有较高的优先级,通常不会被运行时终止,因此可以通过自终止来避免后台运行Service耗费系统的资源。具体方法为在onStartCommand()方法中加入stopSelf();但是要注意的是这里的stopSelf()并不是直接终止Service,而是当Service的所有功能或请求执行完后,将Service停止掉,而不是等待系统回收,停止会调用onDestroy()销毁该Service。
将Service绑定到Activity:
当一个Service在一个Activity中被调用的时候,并不会随着Activity的销毁而销毁,而是仍有可能继续在后台运行着继续占用系统的资源,因此如果实现当Activity销毁时自动停止与其相关的服务,将会极大的节约系统的资源占用,我们可以通过以下方式实现Activity与Service的绑定:
XML布局文件:在该布局文件中实现了四个按钮,分别执行启动Service、停止Service、绑定Service和解除绑定Service,清楚了吧:-)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="demo.chenqian.com.androidserverdemo.MainActivity"> <!-- 开启Service --> <Button android:id="@+id/btnStartService" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" android:text="@string/startService"/> <!-- 关闭Service --> <Button android:id="@+id/btnStopService" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" android:text="@string/stopService"/> <!-- 绑定Service --> <Button android:id="@+id/btnBindService" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" android:text="@string/bindService"/> <!-- 解绑Service --> <Button android:id="@+id/btnUnbindService" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" android:text="@string/unbindService"/> </LinearLayout>
MyService类:
package demo.chenqian.com.androidserverdemo; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.support.annotation.Nullable; import android.util.Log; public class MyService extends Service{
/* 1、在下方我们定义了内部类MyBinder,这就是为什么我们这里现在能定义binder的原因
2、这里我们定义binder成员变量的目的是为了在下文的MainActivity中实现转型*/ private MyBinder binder = new MyBinder(); @Override public void onCreate()