Android 系统中 Activity
承载了界面组件,负责与用户交互,Service
则在后台“默默运行”,给用户提供“服务”,它不与用户直接交互。如果说 Activity
是剧院的大舞台,是呈现“节目”并且负责和“观众互动”,那么Service
就是这个“舞台”的“幕后”,它为现场的活动做“幕后保障”工作。
一起动手实验,认识一下Service
吧!
实验步骤:
ServiceSample
Service
,并配置Service
ServiceSample
首先在 Android Studio
中创建项目名为 ServiceSample
,包名设置为 com.guagua.servicesample
。
Service
,并配置Service
,设置组件的名称为 MyService
。
MyService
代码覆写 onCreate()
、onStartCommand()
、onDestroy()
函数,这里我们对这些函数分别加上日志;函数 onBind()
暂时不做处理。
Java 代码
public class MyService extends Service {
private static final String TAG = "MyService";
@Override
public void onCreate() {
Log.i(TAG, "onCreate()");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, String.format("onStartCommand(flags:%d, startId:%d)", flags, startId));
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy()");
super.onDestroy();
}
......
}
Kotlin 代码
class MyService : Service() {
companion object{
private const val TAG = "MyService"
}
override fun onCreate() {
Log.i(TAG, "onCreate()")
super.onCreate()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.i(TAG, "onStartCommand(flags:$flags, startId:$startId)")
return super.onStartCommand(intent, flags, startId)
}
override fun onDestroy() {
Log.i(TAG, "onDestroy()")
super.onDestroy()
}
......
}
AndroidManifest.xml
清单文件创建好的Service
需要在AndroidManifest.xml
清单文件中注册才能使用:
<application
......
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"/>
</application>
Service
activity_main.xml
<LinearLayout 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:orientation="vertical"
android:gravity="center_horizontal"
android:padding="15dp"
tools:context=".MainActivity">
<Button
android:id="@+id/startBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start" />
<Button
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stop" />
</LinearLayout>
添加了两个按钮,分别用于启动、关闭Service
。
MainActivity
,注册监听事件打开MainActivity
,为刚才增加的按钮注册点击事件监听,
Java 代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startBtn = findViewById(R.id.startBtn);
startBtn.setOnClickListener(view -> {
Log.i(TAG, "startService clicked.");
Intent intent = new Intent(this, MyService.class);
startService(intent);
});
Button stopBtn = findViewById(R.id.stopBtn);
stopBtn.setOnClickListener(view -> {
Log.i(TAG, "stopService clicked.");
Intent intent = new Intent(this, MyService.class);
stopService(intent);
});
}
Kotlin 代码
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val startBtn = findViewById<Button>(R.id.startBtn)
startBtn.setOnClickListener {
Log.i(TAG, "startService clicked.")
val intent = Intent(this, MyService::class.java)
startService(intent)
}
val stopBtn = findViewById<Button>(R.id.stopBtn)
stopBtn.setOnClickListener {
Log.i(TAG, "stopService clicked.")
val intent = Intent(this, MyService::class.java)
stopService(intent)
}
}
这里调用了 startService()
和 stopService()
函数,用于启动、关闭Service
。
Service
,查看日志:
可以看到点击启动按钮后,MyService
被启动,onCreate()
、onStartCommand()
函数被调用.
首先要开启手机的“开发者模式”,然后打开 Settings
→ Developer options
→ Running services
。
我们的项目 ServiceSample
在列表中,点击查看详情:
MyService
正在运行。
Service
看到日志:
每点击一次“start”按钮,就会调用一次 Activity
的 startService()
函数,同时会调用一次MyService
的 onStartCommand()
函数,而onCreate()
不再被调用到。
Service
只在第一次创建启动的时候调用onCreate()
函数,只要该Service
还在运行,就不会再次触发onCreate()
函数;
onStartCommand()
函数则在每次启动Service
的时候都会调用。
Service
点击“stop”按钮,关闭Service
,查看日志:
onDestroy()
函数是在Service
被销毁的时候调用,在Service
被销毁掉了的时候,即使再次“关闭”Service
,也不再触发onDestroy()
函数了。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。