一、定义一个服务。
1、新建ServiceTest项目,右击com.launcher.servicetest→New→Service→Service。
Exported属性表示是否允许除了当前程序外其他程序访问这个服务。
Enabled属性表示是否启用这个服务。
2、重写Service的如下方法。
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
}
//服务创建时调用
@Override
public void onCreate() {
super.onCreate();
}
//每次服务启动时调用
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
//服务销毁时调用
@Override
public void onDestroy() {
super.onDestroy();
}
}
3、在AndroidManifest中注册Service。
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
</service>
二、启动和停止服务。
启动与停止服务主要用Intent来实现。
1、修改activity_main.xml中代码,加入两个Button用于启动与停止服务。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/start_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Service"/>
<Button
android:id="@+id/stop_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop Service"/>
</LinearLayout>
2、修改MainActivity中代码。
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startService = (Button) findViewById(R.id.start_service);
Button stopService = (Button) findViewById(R.id.stop_service);
startService.setOnClickListener(this);
stopService.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.start_service:
Intent startIntent = new Intent(this, MyService.class);
//启动服务
startService(startIntent);
break;
case R.id.stop_service:
Intent stopIntent = new Intent(this, MyService.class);
//停止服务
stopService(stopIntent);
break;
default:
break;
}
}
}
在MyService中调用stopself()方法可以让服务自己自动停下。
onCreate()方法与onStartCommand()方法的区别:
onCreate()方法在服务第一次创建时调用。
onStartCommand()方法在服务每次启动时都会调用。
三、活动与服务之间的通信。
活动与服务之间的通信主要依靠onBind()方法。
实例:MyService提供下载功能同时在活动中决定何时开始下载并随时查看下载进度。
1、修改MyService中代码。
public class MyService extends Service {
private DownloadBinder mBinder = new DownloadBinder();
//新建DownloadBinder类继承自Binder
class DownloadBinder extends Binder{
//开始下载
public void startDownload(){
Log.d("MyService", "startDownload: ");
}
//查看下载进度
public int getProgress(){
Log.d("MyService", "getProgress: ");
return 0;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
……
}
2、修改activity_main.xml中代码,加入两个Button用于绑定与解绑服务。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
……
<Button
android:id="@+id/bind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bind Service"/>
<Button
android:id="@+id/unbind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Unbind Service"/>
</LinearLayout>
3、修改MainActivity中代码。
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private MyService.DownloadBinder downloadBinder;
//匿名类
private ServiceConnection connection = new ServiceConnection() {
//服务与活动绑定成功时调用
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//向下转型实现活动与服务的通信,其中service为onBinder()方法返回的IBinder实例对象。
downloadBinder = (MyService.DownloadBinder) service;
//调用服务中Binder提供的方法。
downloadBinder.startDownload();
downloadBinder.getProgress();
}
//服务与活动解绑时调用
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
…….
Button bindService = (Button) findViewById(R.id.bind_service);
Button unbindService = (Button) findViewById(R.id.unbind_service);
bindService.setOnClickListener(this);
unbindService.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
……
case R.id.bind_service:
Intent bindIntent = new Intent(this, MyService.class);
//绑定服务、其中BIND_AUTO_CREATE表示活动与服务进行绑定后自动创建服务
bindService(bindIntent, connection, BIND_AUTO_CREATE);
break;
case R.id.unbind_service:
//解绑服务
unbindService(connection);
break;
default:
break;
}
}
}
四、服务的生命周期
1、项目的任何位置调用Context的startService()方法,相应的服务就会启动起来,并回调onStartCommand()方法,如果该服务没有被创建过则会先调用onCreate()方法,再调用onStartCommand()方法。
2、当调用Context的bindService()方法后会获取一个服务的持久连接,回调onBind()方法,
如果该服务没有被创建过则会先调用onCreate()方法再调用onBind()方法,调用方可以获取到onBind()方法里返回的IBinder对象实例用于活动与服务间通信。
3、当调用startService()方法后又去调用stopService()方法或调用bindService()方法后又去调用unbindService()方法,这两种情况都会使onDestroy()方法执行。当两种情况同时发生时,必须同时调用stopService()方法与unbindService()方法时onDestroy()方法才会执行。
五、使用前台服务。
前台服务可以使服务一直保持运行状态,而不会由于系统内存不足等原因导致回收。
前台服务与普通服务的区别在于:前台服务会一直有一个正在运行的图标在系统状态栏显示,下拉状态栏后可以看见更加详细的信息。
用法:
调用startForeground()方法,使服务变为前台服务。
第一个参数:通知的id,类似于notify()方法的第一个参数。
第二个参数:构建出的Notification对象。
六、使用IntentService。
服务中代码默认运行在主线程中,如果让服务处理一些耗时任务,则很容易出现ANR。
为了可以简单的创建一个异步的,会自动停止的服务,Android专门提供了一个IntentService类。
1、新建一个MyIntentService继承自IntentService。
public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");
}
//在子线程中运行,不用担心ANR问题。
@Override
protected void onHandleIntent(Intent intent) {
Log.d("MyIntentService", "Thread id is " + Thread.currentThread().getId());
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("MyIntentService", "onDestroy: ");
}
}
2、修改activity_main.xml。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
……
<Button
android:id="@+id/start_intent_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start IntentService"/>
</LinearLayout>
3、修改MainActivity中代码。
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
……
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
……
Button startIntentService = (Button) findViewById(R.id.start_intent_service);
startIntentService.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
……
case R.id.start_intent_service:
//打印主线程id
Log.d("MainActivity", "Thread id is " + Thread.currentThread().getId());
Intent intentService = new Intent(this, MyIntentService.class);
startService(intentService);
break;
default:
break;
}
}
}
4、在AndroidManifest中注册。
<service android:name=".MyIntentService"/>