sharepoint术语教程考试题
作者:互联网
什么是Service
Service又称服务,是一种可在后台执行长耗时操作的无操作界面组件.Service可以被像Activity这样的组件启动,启动后,就在后台一直运行,即使启动它的组件销毁了也不影响Service的运行.启动Service的组件可以和Service进行绑定来进行通信,如播放音乐等.
创建Service
-
在工程目录中单击右键,选择“New>Service>Service”
-
在窗口中设置Service名称(其他设置保持默认即可)
- 创建后的Service如下
class MyService : Service() {
override fun onBind(intent: Intent): IBinder {
TODO("Return the communication channel to the service.")
}
}
- 查看AndroidManifest.xml文件,里面应该已经增加了如下Service配置.
<manifest ...>
<application ...>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>
...
</application>
</manifest>
使用Service
Service有两种使用方式:
- 通过startService启动Service
一旦启动后,服务就在后台执行,即使启动它的组件已销毁也不受影响,除非调用stopService来停止它. - 通过bindService绑定Service
Service被绑定后,一样可以在后台执行,同时还可以和绑定它的组件通信.一个Service可以被多个组件绑定.当所有组件都和它解绑后,那Service就会销毁.
启动Service样例
- Service代码:MyService.kt
class MyService : Service() {
override fun onCreate() {
Log.d("MyService", "onCreate")
super.onCreate()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d("MyService", "onStartCommand")
thread {
study()
}
return super.onStartCommand(intent, flags, startId)
}
private fun study() {
for (index in 1..10) {
Log.d("MyService", "我在学习,我在学习,没偷懒")
Thread.sleep(2000)
}
}
override fun onDestroy() {
Log.d("MyService", "onDestroy")
super.onDestroy()
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
}
- 调用代码
class MainActivity : AppCompatActivity() {
private lateinit var serviceIntent: Intent
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//启动Service
serviceIntent = Intent(this, MyService::class.java)
startService(serviceIntent)
}
override fun onDestroy() {
super.onDestroy()
//停止Service
stopService(serviceIntent)
}
}
绑定Service样例
- Service代码:MyService.kt
class MyService : Service() {
inner class MyBinder : Binder() {
fun study() {
Log.d("MyService", "开始学习")
}
fun getProgress() {
Log.d("MyService", "当前进度:50%")
}
}
private val binder = MyBinder()
override fun onBind(intent: Intent?): IBinder? {
return binder
}
}
- 调用代码
class MainActivity : AppCompatActivity() {
var myBinder: MyService.MyBinder? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initListeners()
}
private fun initListeners() {
//绑定Service
bindBtn.setOnClickListener {
val intent = Intent(this, MyService::class.java)
bindService(intent, serviceConnection, BIND_AUTO_CREATE)
}
//解绑Service
unBindBtn.setOnClickListener { unbindService(serviceConnection) }
//通过绑定关系和Service通信,调用Service里的方法
studyBtn.setOnClickListener { myBinder?.study() }
progressBtn.setOnClickListener { myBinder?.getProgress() }
}
/**
* 创建Service绑定连接
*/
private val serviceConnection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName, binder: IBinder) {
myBinder = binder as MyService.MyBinder
}
override fun onServiceDisconnected(name: ComponentName) {}
}
}
标签:教程,绑定,Service,sharepoint,考试题,override,MyService,fun,class 来源: https://www.cnblogs.com/xbaxzc/p/13886104.html