似乎我是从应用程序启动服务,但未在onStartCommand方法中调用startForeground(),然后将应用程序置于后台,在一分钟后调用onDestroy回调并破坏了服务。
我在Android文档中的任何地方都没有看到此说明。相反,文档含糊地说,最终这种服务将被销毁。
注意onStartCommand方法返回START_STICKY。
任何建议将不胜感激。
后接服务代码;它旨在探索服务功能,而不是作为实际的生产应用程序。同样,编程有时会以不同的方式来做同一件事。我一直在尝试不同的方法以获得更多的见解。而且我对Android还很陌生;后悔天真地写了代码。
class IntegerCounting : Service() {
companion object {
var theCounter : Int = 0
}
private lateinit var mHandler: Handler
private lateinit var mRunnable: Runnable
override fun onBind(intent: Intent): IBinder? {
throw UnsupportedOperationException("Not yet implemented")
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
// Send a notification that service is started
Toast.makeText(getApplicationContext(), "Service started.", Toast.LENGTH_SHORT).show()
// Do a periodic task
mHandler = Handler()
mRunnable = Runnable { CountByOne() }
mHandler.postDelayed(mRunnable, 500)
return Service.START_STICKY
}
override fun onDestroy() {
super.onDestroy()
Toast.makeText(getApplicationContext(), "Service destroyed.", Toast.LENGTH_SHORT).show()
mHandler.removeCallbacks(mRunnable)
}
override fun onTaskRemoved(rootIntent: Intent?) {
Toast.makeText(getApplicationContext(), "On Task Removed called", Toast.LENGTH_SHORT).show()
}
// Custom method to do a task
private fun CountByOne() {
++theCounter
if (theCounter % 10 == 0) {
Toast.makeText(getApplicationContext(), "count by one " + theCounter.toString(), Toast.LENGTH_SHORT).show()
}
//showSomeText(theCounter.toString())
mHandler.postDelayed(mRunnable, 500)
}
}
MainActivity类如下:
class MainActivity : AppCompatActivity() {
// Here is how you find out if an Android service is running
private fun isServiceRunning(serviceClass: Class<*>): Boolean {
val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
// Loop through the running services
for (service in activityManager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.name == service.service.className) {
// If the service is running then return true
return true
}
}
return false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Variable to hold service class name
val serviceClass = IntegerCounting::class.java
// Notice the service class is defined here, so subsequently all
// we need to access this service is this instance of Intent
val intent = Intent(applicationContext, serviceClass)
counterText = findViewById(R.id.counter)
counterText.text = "Ready to start counting"
val startButton: Button = findViewById(R.id.start_counting)
val stopButton: Button = findViewById(R.id.stop_counting)
val showStatus: Button = findViewById(R.id.show_status)
startButton.setOnClickListener({
if (!isServiceRunning(serviceClass)) {
counterText.text = "Starting Integer Counter service"
startService(intent)
} else {
counterText.text = "Integer Counter service already running"
}
})
stopButton.setOnClickListener {
if (isServiceRunning(serviceClass)) {
counterText.text = "Stopping Integer Counter service"
stopService(intent)
} else {
counterText.text = "Integer Counter service already stopped"
}
}
showStatus.setOnClickListener({
if (isServiceRunning(serviceClass)) {
counterText.text = "Integer Counter service is running, counter: " +
IntegerCounting.theCounter.toString()
} else {
counterText.text = "Integer Counter service is stopped, counter " +
IntegerCounting.theCounter.toString()
}
})
}
}
Service
,请尝试将startForceground
与Notification
结合使用>
虽然应用程序位于前台,但它可以自由创建和运行前台和后台服务。当应用进入后台时,它会有几分钟的窗口,仍允许其创建和使用服务。在该窗口结束时,该应用程序被认为是空闲的。这时,系统停止应用程序的后台服务,就像该应用程序已调用服务的Service.stopSelf()方法一样。文档here