Java 舱单广播接收器不再工作
我在清单中设置了一个广播接收器,通过toast和log消息告诉我蓝牙何时开启或关闭。接收器在棉花糖和馅饼上都工作正常,但我需要应用程序在后台工作,所以我设置了一个持续的通知。在执行持续通知后,我现在可以在关闭运行棉花糖的设备上的应用程序后接收我的toast/log消息。我尝试在运行Pie的设备上测试应用程序,但我不再从接收器接收toast/log消息,即使应用程序已打开;我想它完全停止工作了。我读了日志,没有看到任何有用的东西 AndroidManifest.xmlJava 舱单广播接收器不再工作,java,android,kotlin,Java,Android,Kotlin,我在清单中设置了一个广播接收器,通过toast和log消息告诉我蓝牙何时开启或关闭。接收器在棉花糖和馅饼上都工作正常,但我需要应用程序在后台工作,所以我设置了一个持续的通知。在执行持续通知后,我现在可以在关闭运行棉花糖的设备上的应用程序后接收我的toast/log消息。我尝试在运行Pie的设备上测试应用程序,但我不再从接收器接收toast/log消息,即使应用程序已打开;我想它完全停止工作了。我读了日志,没有看到任何有用的东西 AndroidManifest.xml MainActivity
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
class BReceivers : BroadcastReceiver() {
companion object {
const val TAG = "Bluetoooth"
}
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
when(intent?.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
BluetoothAdapter.STATE_OFF -> Log.d(TAG, "Bluetooth is OFF")
BluetoothAdapter.STATE_TURNING_OFF -> Toast.makeText(context, "Bluetooth is turning off", Toast.LENGTH_LONG).show()
BluetoothAdapter.STATE_ON -> Log.d(TAG, "Bluetooth is ON")
BluetoothAdapter.STATE_TURNING_ON -> Toast.makeText(context, "Bluetooth is turning ON", Toast.LENGTH_LONG).show()
}
}
}
}
import android.app.Application
import android.content.Intent
class OngoingApp : Application() {
override fun onCreate() {
super.onCreate()
startService(Intent(this, OngoingService::class.java))
}
}
import android.app.*
import android.content.Intent
import android.os.IBinder
import android.support.v4.app.NotificationCompat
import android.content.Context
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v4.app.NotificationCompat.PRIORITY_MIN
class OngoingService : Service() {
override fun onBind(intent: Intent): IBinder? {
return null
}
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
startForeground()
return super.onStartCommand(intent, flags, startId)
}
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun startForeground() {
val channelId =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
initChannels("fearApp_service", "Fear Appeals Service")
} else {
// If earlier version channel ID is not used
// https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context)
""
}
val notificationIntent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this, 0,
notificationIntent, 0
)
val notificationBuilder = NotificationCompat.Builder(this, channelId )
val notification = notificationBuilder
.setOngoing(true)
.setContentTitle(getString(R.string.app_name))
.setContentText("Fear Appeals is running background")
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(PRIORITY_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.setContentIntent(pendingIntent)
.build()
startForeground(101, notification)
}
@RequiresApi(Build.VERSION_CODES.O)
private fun initChannels(channelId: String, channelName: String): String {
val channel = NotificationChannel(
channelId,
channelName,
NotificationManager.IMPORTANCE_DEFAULT)
channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
channel.description = "Ongoing notification to keep the app opened."
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
return channelId
}
}
BReceivers.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
class BReceivers : BroadcastReceiver() {
companion object {
const val TAG = "Bluetoooth"
}
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
when(intent?.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
BluetoothAdapter.STATE_OFF -> Log.d(TAG, "Bluetooth is OFF")
BluetoothAdapter.STATE_TURNING_OFF -> Toast.makeText(context, "Bluetooth is turning off", Toast.LENGTH_LONG).show()
BluetoothAdapter.STATE_ON -> Log.d(TAG, "Bluetooth is ON")
BluetoothAdapter.STATE_TURNING_ON -> Toast.makeText(context, "Bluetooth is turning ON", Toast.LENGTH_LONG).show()
}
}
}
}
import android.app.Application
import android.content.Intent
class OngoingApp : Application() {
override fun onCreate() {
super.onCreate()
startService(Intent(this, OngoingService::class.java))
}
}
import android.app.*
import android.content.Intent
import android.os.IBinder
import android.support.v4.app.NotificationCompat
import android.content.Context
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v4.app.NotificationCompat.PRIORITY_MIN
class OngoingService : Service() {
override fun onBind(intent: Intent): IBinder? {
return null
}
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
startForeground()
return super.onStartCommand(intent, flags, startId)
}
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun startForeground() {
val channelId =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
initChannels("fearApp_service", "Fear Appeals Service")
} else {
// If earlier version channel ID is not used
// https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context)
""
}
val notificationIntent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this, 0,
notificationIntent, 0
)
val notificationBuilder = NotificationCompat.Builder(this, channelId )
val notification = notificationBuilder
.setOngoing(true)
.setContentTitle(getString(R.string.app_name))
.setContentText("Fear Appeals is running background")
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(PRIORITY_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.setContentIntent(pendingIntent)
.build()
startForeground(101, notification)
}
@RequiresApi(Build.VERSION_CODES.O)
private fun initChannels(channelId: String, channelName: String): String {
val channel = NotificationChannel(
channelId,
channelName,
NotificationManager.IMPORTANCE_DEFAULT)
channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
channel.description = "Ongoing notification to keep the app opened."
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
return channelId
}
}
OngoingApp.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
class BReceivers : BroadcastReceiver() {
companion object {
const val TAG = "Bluetoooth"
}
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
when(intent?.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
BluetoothAdapter.STATE_OFF -> Log.d(TAG, "Bluetooth is OFF")
BluetoothAdapter.STATE_TURNING_OFF -> Toast.makeText(context, "Bluetooth is turning off", Toast.LENGTH_LONG).show()
BluetoothAdapter.STATE_ON -> Log.d(TAG, "Bluetooth is ON")
BluetoothAdapter.STATE_TURNING_ON -> Toast.makeText(context, "Bluetooth is turning ON", Toast.LENGTH_LONG).show()
}
}
}
}
import android.app.Application
import android.content.Intent
class OngoingApp : Application() {
override fun onCreate() {
super.onCreate()
startService(Intent(this, OngoingService::class.java))
}
}
import android.app.*
import android.content.Intent
import android.os.IBinder
import android.support.v4.app.NotificationCompat
import android.content.Context
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v4.app.NotificationCompat.PRIORITY_MIN
class OngoingService : Service() {
override fun onBind(intent: Intent): IBinder? {
return null
}
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
startForeground()
return super.onStartCommand(intent, flags, startId)
}
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun startForeground() {
val channelId =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
initChannels("fearApp_service", "Fear Appeals Service")
} else {
// If earlier version channel ID is not used
// https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context)
""
}
val notificationIntent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this, 0,
notificationIntent, 0
)
val notificationBuilder = NotificationCompat.Builder(this, channelId )
val notification = notificationBuilder
.setOngoing(true)
.setContentTitle(getString(R.string.app_name))
.setContentText("Fear Appeals is running background")
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(PRIORITY_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.setContentIntent(pendingIntent)
.build()
startForeground(101, notification)
}
@RequiresApi(Build.VERSION_CODES.O)
private fun initChannels(channelId: String, channelName: String): String {
val channel = NotificationChannel(
channelId,
channelName,
NotificationManager.IMPORTANCE_DEFAULT)
channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
channel.description = "Ongoing notification to keep the app opened."
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
return channelId
}
}
OngoingService.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
class BReceivers : BroadcastReceiver() {
companion object {
const val TAG = "Bluetoooth"
}
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
when(intent?.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
BluetoothAdapter.STATE_OFF -> Log.d(TAG, "Bluetooth is OFF")
BluetoothAdapter.STATE_TURNING_OFF -> Toast.makeText(context, "Bluetooth is turning off", Toast.LENGTH_LONG).show()
BluetoothAdapter.STATE_ON -> Log.d(TAG, "Bluetooth is ON")
BluetoothAdapter.STATE_TURNING_ON -> Toast.makeText(context, "Bluetooth is turning ON", Toast.LENGTH_LONG).show()
}
}
}
}
import android.app.Application
import android.content.Intent
class OngoingApp : Application() {
override fun onCreate() {
super.onCreate()
startService(Intent(this, OngoingService::class.java))
}
}
import android.app.*
import android.content.Intent
import android.os.IBinder
import android.support.v4.app.NotificationCompat
import android.content.Context
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v4.app.NotificationCompat.PRIORITY_MIN
class OngoingService : Service() {
override fun onBind(intent: Intent): IBinder? {
return null
}
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
startForeground()
return super.onStartCommand(intent, flags, startId)
}
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun startForeground() {
val channelId =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
initChannels("fearApp_service", "Fear Appeals Service")
} else {
// If earlier version channel ID is not used
// https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context)
""
}
val notificationIntent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this, 0,
notificationIntent, 0
)
val notificationBuilder = NotificationCompat.Builder(this, channelId )
val notification = notificationBuilder
.setOngoing(true)
.setContentTitle(getString(R.string.app_name))
.setContentText("Fear Appeals is running background")
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(PRIORITY_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.setContentIntent(pendingIntent)
.build()
startForeground(101, notification)
}
@RequiresApi(Build.VERSION_CODES.O)
private fun initChannels(channelId: String, channelName: String): String {
val channel = NotificationChannel(
channelId,
channelName,
NotificationManager.IMPORTANCE_DEFAULT)
channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
channel.description = "Ongoing notification to keep the app opened."
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
return channelId
}
}