清单广播接收器不再工作

时间:2019-05-30 15:35:58

标签: java android kotlin

我在清单中设置了一个广播接收器,以通过烤面包和日志消息告诉我蓝牙何时打开或关闭。接收器在棉花糖和Pie上都可以正常工作,但是我需要在后台运行该应用程序,因此我设置了持续的通知。实施正在进行的通知后,在运行Marshmallow的设备上关闭应用程序后,我现在可以接收烤面包/日志消息。我试图在运行Pie的设备上测试该应用程序,但是即使打开该应用程序,我也无法再从接收器接收到Toast / Log消息。我认为它完全停止工作了。我读了logcat,没有发现任何帮助。

AndroidManifest.xml

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name=".OngoingApp">
        <service android:name=".NotificationsMessagingService">
            <intent-filter android:priority="1">
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <service android:name=".OngoingService" />

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".BReceivers" android:exported="true" android:enabled="true">
            <intent-filter>
                <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
            </intent-filter>
        </receiver>
    </application>

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

    }
}

BReceivers.kt

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()
            }
        }

    }

}

OngoingApp.kt

import android.app.Application
import android.content.Intent

class OngoingApp : Application() {
    override fun onCreate() {
        super.onCreate()

        startService(Intent(this, OngoingService::class.java))
    }
}

OngoingService.kt

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
    }
}

0 个答案:

没有答案