Service是一个可以 在后台长时间操作而不提供用户界面的应用组件。服务有两种形式:启动,绑定。
启动:服务可由其他应用组件启动,而且即使用户切换到其他它应用,服务任然可以在后台继续运行。
绑定:组件可以绑定到服务,之间可以进行交互,甚至是执行进程间通信(IPC)
服务的生命周期
onCreate onStartCommand onDestroy onBind
今天先给大家来讲讲启动
当应用组件(如 Activity)通过调用startService()启动服务时,服务即处于“启动状态”。一旦启动服务即可在后台无期限运行,即使启动服务的应用组件被销毁也不受影响。已经启动的服务通常是执行单一操作,而不会将结果返回给调用方。
首先写一个界面,里面两个按钮,写一个onClick方法
<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.zking.hgz_android_22_service.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="start"
android:text="启动服务"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="stop"
android:text="停止服务"/>
</LinearLayout>
package com.zking.hgz_android_22_service;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this,MyService.class);
}
public void start(View view){
startService(intent);
}
public void stop(View view){
stopService(intent);
}
}
写一个类继承Service
package com.zking.hgz_android_22_service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Created by hgz on 2017/2/12.
*/
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i("text","onBind");
return null;
}
@Override
public void onCreate() {
Log.i("text","onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("text","onStartCommand");
new MyTherad(startId).start();
return super.onStartCommand(intent, flags, startId);
}
class MyTherad extends Thread{
public int startId;
public MyTherad(int startId) {
this.startId = startId;
}
@Override
public void run() {
super.run();
//耗时操作
for (int i = 0; i < 10; i++) {
Log.i("text","i="+i);
SystemClock.sleep(1000);
// //停止服务,当第一个线程结束则停止服务
// stopSelf();
//
// //所有线程结束后再停止服务
stopSelf(startId);
}
}
}
@Override
public void onDestroy() {
Log.i("text","onDestroy");
super.onDestroy();
}
}
服务虽然说是执行耗时操作的,但是执行耗时操作时也要用到线程,在这里服务和线程的区别就像将军与士兵一样,将军是指挥的不容易死,但是士兵呢永远是冲在最前面的所以容易死。
配置清单
<service android:name=".MyService"
android:exported="true"></service>
服务还可以继承IntentService,但是里面只有一个onHandleIntent方法,类似Service中的onStartCommand方法。它执行耗时操作不需要用到线程,里面自带有线程,并且有消息队列,执行完第一个在执行第二个。但是继承IntentService在里面必须要写构造方法,一个有参数,一个无参数,在无参调用有参的super(“”);
package com.zking.hgz_android_22_service;
import android.app.IntentService;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;
/**
* Created by hgz on 2017/2/12.
*/
public class MyService02 extends IntentService {
public MyService02() {
super("");
}
public MyService02(String name) {
super(name);
}
//类似Service中的onStartCommand方法
@Override
protected void onHandleIntent(Intent intent) {
for (int i = 0; i <10; i++) {
Log.i("text","i="+i);
SystemClock.sleep(500);
}
}
}