Service也是安卓的四大组件之一,它与Activity都继承了Context,创建一个Service时也需要在清单文件中声明,
,如果希望服务不仅仅在该应用中使用时,那么就要在清单文件的service标签内添加
1 | <intent-filter> |
但与Activity最大的区别就是没有界面,通常用来常驻后台处理一些事情,比如推送服务就是常驻后台用来像用户不定时推送消息。service不是线程。
Service有两种启动方式,本文讲述第一种,Start Service。
Start Service的生命周期很简单,只通过onCreate(),onStart()和onDestroy()来控制。
onCreate()
在Service被创建的时候调用onStart()
在Service启动的时候调用onDestroy()
在Service终止的时候调用
下面通过代码分析:
ServiceDemo.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41package com.example.ahtcfg24.startservicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class ServiceDemo extends Service {
public static final String TAG = "ServiceDemo";
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "--->onCreate");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "--->onDestroy");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i(TAG, "--->onStart");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "--->onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
/*onBinder()是父类中的抽象方法,必须被复写*/
@Override
public IBinder onBind(Intent intent) {
//通过StartService启动无需返回任何东西
return null;
}
}
MainActivity.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46package com.example.ahtcfg24.startservicedemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 启动Service
*/
public class MainActivity extends Activity {
private Button button;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new ButtonListener());
button2.setOnClickListener(new ButtonListener());
}
private class ButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ServiceDemo.class);
switch (v.getId()) {
case R.id.button:
startService(intent);//启动Service
break;
case R.id.button2:
stopService(intent);//终止Service
break;
default:
break;
}
}
}
}
activity_main.xml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24<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"
android:background="#637fbb"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="77dp"
android:text="启动Service"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:text="停止Service"/>
</RelativeLayout>
查看代码
依次点击两个按钮
通过AndroidStudio的logcat过滤查看结果:
再多点几次点击启动
然而不点结束,在设置中查看正在运行的应用,如图
结果分析:
- Service在StartService()之后执行onCreate方法,随后执行onStart方法,当Service已经被创建时,再执行StartService()也不会执行onCreate,但是还会执行onStart方法。
- 当执行了stopService()之后,service执行onDestroy方法,service终止。
- 另外
onStartCommand()
在新版的sdk中已经取代了onStart()
,调用onStart()之前会先调用onStartCommand(),本文不讨论onStartCommand()