0

I have try to learn Android jetpack compose.
I am trying to use NotificationServiceListener with accompanist permission library, but it does not work correctly.

I have added the <service> element on AndroidManifest.xml, And the name of Listener class is same as a class name that mentioned in AndroidManifest.xml

I have no idea why it does not work.

Nothing happen even tap the button. there is no any display to ask to user whether allow it despite launchPermissionRequest is invoked in onclick event.


// Listener Class
class MyListener : NotificationListenerService() {
    override fun onNotificationPosted(sbn: StatusBarNotification?) {
        super.onNotificationPosted(sbn)
        Log.i("Notification Listener", sbn!!.opPkg)
    }
}


// Main Activity Code
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            LineLight2Theme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Column(modifier = Modifier
                        .fillMaxWidth()
                        .fillMaxHeight(),
                            verticalArrangement = Arrangement.Center,
                            horizontalAlignment = Alignment.CenterHorizontally) {
                        BigButton()
                    }
                }
            }
        }
    }
}


@Preview(showBackground = true)
@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun BigButton() {
    val servicePermissionState = rememberPermissionState(permission = android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE)
    val listener = MyListener()
    var is_enable = remember { mutableStateOf(true)}
    val text = if (servicePermissionState.status.isGranted) "Enable" else "Disable"
    var color = if (is_enable.value) Color.DarkGray else Color.Red
    val context = LocalContext.current
    if (!servicePermissionState.status.isGranted) {
        color = Color.Red
    }
    Button(modifier= Modifier
        .size(width = 220.dp, height = 80.dp),
        colors=ButtonDefaults.buttonColors(backgroundColor = color), onClick = {
            if (servicePermissionState.status.isGranted) {
                is_enable.value = !is_enable.value
            } else {
                servicePermissionState.launchPermissionRequest()
            }
        }) {
        Text(text)
    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.LineLight2"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.LineLight2">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
        <service android:name=".MyListener"
            android:exported="true"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>
    </application>

</manifest>
KiYugadgeter
  • 3,796
  • 7
  • 34
  • 74

0 Answers0