行
行 用
Foreground Service
24
Ҿ༻:https://developer.android.com/develop/background-work/services/fgs
Slide 25
Slide 25 text
No content
Slide 26
Slide 26 text
Jetpack Compose
IP
ConnectivityManager
IP
用
UI
26
private fun getCurrentIpAddress(): String {
val connectivityManager =
getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val currentNetwork = connectivityManager.activeNetwork
val linkProperties = connectivityManager.getLinkProperties(currentNetwork)
return linkProperties?.linkAddresses?.first {
it.address is Inet4Address
}.toString()
}
Slide 27
Slide 27 text
曰
LinkProperties IP
Android API
行
linkProperties
見
27
Ҿ༻:
https://developer.android.com/develop/connectivity/network-ops/reading-network-state
https://developer.android.com/reference/android/net/LinkProperties#getLinkAddresses()
Slide 28
Slide 28 text
行
Foreground Service
用
Service
用
Foreground
Foreground Service
行
28
Slide 29
Slide 29 text
行
Foreground Service
用
Service
用
Foreground
Foreground Service
行
29
Slide 30
Slide 30 text
Activity
用
override
Service
用
30
class FtpService : Service() {
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
// ࣮ࡍʹαʔϏεʹ͍ͤͨ͞ॲཧΛ͜͜ʹهड़͢Δ
return START_STICKY
}
override fun onDestroy() {
super.onDestroy()
}
}
Slide 31
Slide 31 text
行
Foreground Service
用
Service
用
Foreground
Foreground Service
行
31
Slide 32
Slide 32 text
Service Activity Manifest
見
Foreground Service
言
32
Ҿ༻:https://developer.android.com/develop/background-work/services/fgs/declare#declare-fgs
Slide 33
Slide 33 text
foregroundServiceType
Android 14 API 34
Foreground Service
33
Ҿ༻:https://developer.android.com/develop/background-work/services/fgs/service-types
Slide 34
Slide 34 text
foregroundServiceType
Android 14 API 34
色
Data sync
Foreground Service
34
Ҿ༻:https://developer.android.com/develop/background-work/services/fgs/service-types#data-sync
Slide 35
Slide 35 text
見
Android Manifest
Manifest Foreground Service
言
35
…
Slide 36
Slide 36 text
行
Foreground Service
用
Service
用
Foreground
Foreground Service
行
36
Slide 37
Slide 37 text
Service onStartCommand
行 用
ServiceCompat.startForeground
行
用
Foreground Service
37
Ҿ༻:https://developer.android.com/develop/background-work/services/fgs/launch#promote-service
Slide 38
Slide 38 text
Foreground Service
用
Foreground Service
38
try {
ServiceCompat.startForeground(
this,
ID_FOREGROUND_SERVICE_FTP,
createNotification(), // ௨Λ࡞Δؔʢ࣍ϖʔδʣ
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
} else {
0
} // ManifestͰએݴͨ͠Foreground Service TypeΛͯࠐΉ
)
// TODO: Foregroundʹग़དྷͨΒ͜͜ͰFTPαʔόΛ࣮ߦ͍ͨ͠
} catch (e: Exception) {
Timber.e(e, "Failed to launch foreground service”)
}
Slide 39
Slide 39 text
用
Foreground Service
39
private fun createNotification(): Notification {
val channel = NotificationChannel(
NOTIFICATION_CHANNEL_ID,
NOTIFICATION_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT
)
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel) // ௨νϟωϧͷઃఆ
val notification = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID).apply {
setContentTitle("FTP Server")
setContentText("FTP Server established")
setSmallIcon(R.drawable.notification_icon)
}.build() // ࣮ࡍͷ௨ͷੜ
return notification
}
Slide 40
Slide 40 text
Activity ForegroundService
行
行
Foreground Service
40
private fun launchFtpService() {
val intent = Intent(this, FtpService::class.java)
startForegroundService(intent)
}
Slide 41
Slide 41 text
Apache FTP Server
見
立 方
FTP
方
Foreground Service FTP
行
41