英文:
How to create long time socket in background android
问题
我想使用套接字编程创建自定义推送通知服务。
我遇到的问题是,我希望套接字在后台保持活动状态。但是在应用程序被关闭后,所有的后台服务都被禁用了。我也不想使用前台服务。你有解决方案吗?
清单文件:
<service android:name="com.example.app.CustomNotificationService">
</service>
服务类:
class CustomNotificationService : Service() {
override fun onCreate() {
AndroidInjection.inject(this);
super.onCreate()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Thread{
while (true){
Thread.sleep(10000)
mHandler.post {
toast("a")
}
}
}.start()
return START_STICKY
}
override fun onDestroy() {
super.onDestroy()
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
}
活动类:
startService(Intent(this@MainActivity,CustomNotificationService::class.java))
英文:
I want to create custom push notification service with socket programming.
The problem I have is that I want the socket to be constantly active in background. But all my background services are disabled after the app is killed. I also do not want to use foreground service. Do you have a solution?
Manifrest:
<service android:name="com.example.app.CustomNotificationService">
</service>
Serivce:
class CustomNotificationService : Service() {
override fun onCreate() {
AndroidInjection.inject(this);
super.onCreate()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Thread{
while (true){
Thread.sleep(10000)
mHandler.post {
toast("a")
}
}
}.start()
return START_STICKY
}
override fun onDestroy() {
super.onDestroy()
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
}
Activity:
startService(Intent(this@MainActivity,CustomNotificationService::class.java))
专注分享java语言的经验与见解,让所有开发者获益!
评论