unbindService 会在 Android Studio 中自动清除通知图标吗?
Will unbindService clean Notification icon automatically in Android Studio?
以下代码来自project.
启动时会显示通知图标bindService()
,没关系。
但是启动时通知图标不会被清除unbindService()
,为什么?我在以下代码中找不到任何代码来清理通知图标。
MainActivity.kt
class MainActivity : AppCompatActivity(), View.OnClickListener {
val mTAG = MainActivity::class.java.simpleName
// Variable for storing instance of our service class
var mService: MyBoundService? = null
// Boolean to check if our activity is bound to service or not
var mIsBound: Boolean? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
startServiceButton?.setOnClickListener(this)
stopServiceButton?.setOnClickListener(this)
startActivityButton?.setOnClickListener(this)
}
private val serviceConnection = object : ServiceConnection {
override fun onServiceConnected(className: ComponentName, iBinder: IBinder) {
Log.d(mTAG, "ServiceConnection: connected to service.")
// We've bound to MyService, cast the IBinder and get MyBinder instance
val binder = iBinder as MyBinder
mService = binder.service
mIsBound = true
getRandomNumberFromService() // return a random number from the service
}
override fun onServiceDisconnected(arg0: ComponentName) {
Log.d(mTAG, "ServiceConnection: disconnected from service.")
mIsBound = false
}
}
/**
* Method for listening to random numbers generated by our service class
*/
private fun getRandomNumberFromService() {
mService?.randomNumberLiveData?.observe(this
, Observer {
resultTextView?.text = "Random number from service: $it"
})
}
override fun onDestroy() {
super.onDestroy()
// Unbinding to the service class
unbindService()
}
private fun bindService() {
Intent(this, MyBoundService::class.java).also { intent ->
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
}
}
private fun unbindService() {
Intent(this, MyBoundService::class.java).also { intent ->
unbindService(serviceConnection)
}
}
override fun onClick(v: View?) {
when (v?.id) {
R.id.startServiceButton -> {
bindService()
}
R.id.stopServiceButton -> {
if (mIsBound == true) {
unbindService()
mIsBound = false
}
}
R.id.startActivityButton -> {
val intent = Intent(this, ResultActivity::class.java)
startActivity(intent)
}
}
}
}
MyBoundService.kt
class MyBoundService : Service() {
...
override fun onBind(intent: Intent): IBinder? {
return mBinder
}
override fun onCreate() {
super.onCreate()
Log.d("MyBoundService", "onCreate called")
startNotification()
Handler().postDelayed({
val randomNumber = mGenerator.nextInt(100)
randomNumberLiveData.postValue(randomNumber)
}, 1000)
}
private fun startNotification() {
val channel = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel(
CHANNEL_ID,
"My Channel",
NotificationManager.IMPORTANCE_DEFAULT
)
} else {
TODO("VERSION.SDK_INT < O")
}
(getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(channel)
val notification: Notification = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("A service is running in the background")
.setContentText("Generating random number").build()
startForeground(1, notification)
}
}
是的,MyBoundService
的代码调用 startForeground()
,它允许 MyBoundService
继续 运行,即使 MainActivity
在前景,直到 Android OS 出于释放内存等原因决定停止 MyBoundService
。
要停止 MyBoundService
并删除通知,需要调用 stopForeground(true)
,然后调用 stopSelf()
。您可以通过添加以下内容来使用您拥有的示例代码来实现这一点:
MainActivity.kt
private fun unbindService() {
Intent(this, MyBoundService::class.java).also { intent ->
unbindService(serviceConnection)
mService.stopForeground(true)
mService.stopSelf()
}
}
您正在启动前台服务
请检查这个link 停止服务运行作为前台的正确方法是什么
此外,您可能需要查看 Stopping a service
以下代码来自project.
启动时会显示通知图标bindService()
,没关系。
但是启动时通知图标不会被清除unbindService()
,为什么?我在以下代码中找不到任何代码来清理通知图标。
MainActivity.kt
class MainActivity : AppCompatActivity(), View.OnClickListener {
val mTAG = MainActivity::class.java.simpleName
// Variable for storing instance of our service class
var mService: MyBoundService? = null
// Boolean to check if our activity is bound to service or not
var mIsBound: Boolean? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
startServiceButton?.setOnClickListener(this)
stopServiceButton?.setOnClickListener(this)
startActivityButton?.setOnClickListener(this)
}
private val serviceConnection = object : ServiceConnection {
override fun onServiceConnected(className: ComponentName, iBinder: IBinder) {
Log.d(mTAG, "ServiceConnection: connected to service.")
// We've bound to MyService, cast the IBinder and get MyBinder instance
val binder = iBinder as MyBinder
mService = binder.service
mIsBound = true
getRandomNumberFromService() // return a random number from the service
}
override fun onServiceDisconnected(arg0: ComponentName) {
Log.d(mTAG, "ServiceConnection: disconnected from service.")
mIsBound = false
}
}
/**
* Method for listening to random numbers generated by our service class
*/
private fun getRandomNumberFromService() {
mService?.randomNumberLiveData?.observe(this
, Observer {
resultTextView?.text = "Random number from service: $it"
})
}
override fun onDestroy() {
super.onDestroy()
// Unbinding to the service class
unbindService()
}
private fun bindService() {
Intent(this, MyBoundService::class.java).also { intent ->
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
}
}
private fun unbindService() {
Intent(this, MyBoundService::class.java).also { intent ->
unbindService(serviceConnection)
}
}
override fun onClick(v: View?) {
when (v?.id) {
R.id.startServiceButton -> {
bindService()
}
R.id.stopServiceButton -> {
if (mIsBound == true) {
unbindService()
mIsBound = false
}
}
R.id.startActivityButton -> {
val intent = Intent(this, ResultActivity::class.java)
startActivity(intent)
}
}
}
}
MyBoundService.kt
class MyBoundService : Service() {
...
override fun onBind(intent: Intent): IBinder? {
return mBinder
}
override fun onCreate() {
super.onCreate()
Log.d("MyBoundService", "onCreate called")
startNotification()
Handler().postDelayed({
val randomNumber = mGenerator.nextInt(100)
randomNumberLiveData.postValue(randomNumber)
}, 1000)
}
private fun startNotification() {
val channel = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel(
CHANNEL_ID,
"My Channel",
NotificationManager.IMPORTANCE_DEFAULT
)
} else {
TODO("VERSION.SDK_INT < O")
}
(getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(channel)
val notification: Notification = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("A service is running in the background")
.setContentText("Generating random number").build()
startForeground(1, notification)
}
}
是的,MyBoundService
的代码调用 startForeground()
,它允许 MyBoundService
继续 运行,即使 MainActivity
在前景,直到 Android OS 出于释放内存等原因决定停止 MyBoundService
。
要停止 MyBoundService
并删除通知,需要调用 stopForeground(true)
,然后调用 stopSelf()
。您可以通过添加以下内容来使用您拥有的示例代码来实现这一点:
MainActivity.kt
private fun unbindService() {
Intent(this, MyBoundService::class.java).also { intent ->
unbindService(serviceConnection)
mService.stopForeground(true)
mService.stopSelf()
}
}
您正在启动前台服务
请检查这个link 停止服务运行作为前台的正确方法是什么
此外,您可能需要查看 Stopping a service