在附加堆栈跟踪时获取了Android a资源,但从未释放

kqqjbcuj  于 2023-04-18  发布在  Android
关注(0)|答案(1)|浏览(114)

我写了下面的代码,这样我就可以阅读通知,并相应地执行一些操作,但我得到了下面的错误:

2022-03-17 18:25:54.513 17447-17469/cocoon.notification I/AdrenoGLES-0: QUALCOMM build                   : 5fa70b0888, I2c4b6824f1
    Build Date                       : 09/13/21
    OpenGL ES Shader Compiler Version: EV031.32.02.15
    Local Branch                     : 
    Remote Branch                    : 
    Remote Branch                    : 
    Reconstruct Branch               : 
2022-03-17 18:25:54.513 17447-17469/cocoon.notification I/AdrenoGLES-0: Build Config                     : S P 10.0.7 AArch64
2022-03-17 18:25:54.513 17447-17469/cocoon.notification I/AdrenoGLES-0: Driver Path                      : /vendor/lib64/egl/libGLESv2_adreno.so
2022-03-17 18:25:54.518 17447-17469/cocoon.notification I/AdrenoGLES-0: PFP: 0x016ee190, ME: 0x00000000
2022-03-17 18:25:54.527 17447-17469/cocoon.notification D/hw-ProcessState: Binder ioctl to enable oneway spam detection failed: Invalid argument
2022-03-17 18:25:54.541 17447-17469/cocoon.notification E/on.notificatio: open libmigui.so failed! dlopen - dlopen failed: library "libmigui.so" not found
2022-03-17 18:25:54.571 17447-17447/cocoon.notification D/DecorView[]: onWindowFocusChanged hasWindowFocus true
2022-03-17 18:25:56.217 17447-17459/cocoon.notification D/StrictMode: StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. Callsite: close
        at android.os.StrictMode$AndroidCloseGuardReporter.report(StrictMode.java:1992)
        at dalvik.system.CloseGuard.warnIfOpen(CloseGuard.java:347)
        at sun.nio.fs.UnixSecureDirectoryStream.finalize(UnixSecureDirectoryStream.java:580)
        at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:291)
        at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:278)
        at java.lang.Daemons$Daemon.run(Daemons.java:139)
        at java.lang.Thread.run(Thread.java:920)
2022-03-17 18:26:08.230 17447-17447/cocoon.notification D/DecorView[]: onWindowFocusChanged hasWindowFocus false

我的代码是:

// MainActivity.kt

class MainActivity : ComponentActivity() {
    private var context: Context? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if(BuildConfig.DEBUG) {
            StrictMode.enableDefaults()
          /*  StrictMode.setVmPolicy(
                VmPolicy.Builder()
                    .detectAll()
                    .penaltyLog() //                    .penaltyDeath()
                    .build()
            ) */
        }

        context = applicationContext
        setContent {
            NotificationTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    Greeting("Android")
                }
            }
        }
        context?.also {
            //  val cn = it?.let { it1 -> ComponentName(it1, NotificationListener::class.java) }
            val cn = ComponentName(it, NotificationListener::class.java)
            val flat: String = Settings.Secure.getString(
                it.contentResolver,
                "enabled_notification_listeners"
            )
            val enabled = flat.contains(cn.flattenToString()) // flat != null &&
            if (!enabled) showPermissionDialog()
        }
    }

    private fun showPermissionDialog() = context?.startActivity(
        Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS").addFlags(
            Intent.FLAG_ACTIVITY_NEW_TASK
        )
    )
}

@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    NotificationTheme {
        Greeting("Android")
    }
}

并且:

// NotificationListener.kt

class NotificationListener : NotificationListenerService() {
    private var context: Context? = null
    override fun onCreate() {
        super.onCreate()
        context = applicationContext
    }

    override fun onNotificationPosted(sbn: StatusBarNotification) {
        super.onNotificationPosted(sbn)
        val packageName = sbn.packageName
        var nTicker = ""
        if (sbn.notification.tickerText != null) {
            nTicker = sbn.notification.tickerText.toString()
        }
        val extras = sbn.notification.extras
        val title = extras.getString("android.title")
        val text = extras.getCharSequence("android.text").toString()
        // val id1 = extras.getInt(Notification.BADGE_ICON_SMALL.toString()) /* .EXTRA_SMALL_ICON */
        val id = sbn.notification.getLargeIcon() //   .largeIcon;
        Log.i("Package", packageName)
        Log.i("Ticker", nTicker)
        if (title != null) {
            Log.i("Title", title)
        }
        Log.i("Text", text)
        val msgInfo = Intent("Msg")
        msgInfo.putExtra("package", packageName)
        msgInfo.putExtra("nTicker", nTicker)
        msgInfo.putExtra("title", title)
        msgInfo.putExtra("text", text)
        when (packageName) {
            "com.whatsapp.w4b" -> {
                Toast.makeText(context, "whatsapp business $text from $title", Toast.LENGTH_SHORT).show()

                val msg = "text to send"

                val sendIntent = Intent(Intent.ACTION_SEND)
                sendIntent.type = "text/plain"
                sendIntent.putExtra(Intent.EXTRA_TEXT, msg)
                sendIntent.putExtra(
                    "jid", title // the (Whatsapp) phone number of contact
                )
                sendIntent.setPackage("com.whatsapp.w4b")
                //  sendIntent.putExtra("jid", recipient+"@s.whatsapp.net")
                sendIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                startActivity(sendIntent)
            }
             "com.whatsapp" -> {
                 if (nTicker.isNullOrEmpty()) {
                     Log.i("Group msg", "yes")
                 } else {
                     Log.i("Group msg", "no")
                 }
                 /*   Toast.makeText(context, "whatsapp $text from $title", Toast.LENGTH_SHORT).show()

                    val msg = "text to send"

                    val sendIntent = Intent(Intent.ACTION_SEND)
                    sendIntent.type = "text/plain"
                    sendIntent.putExtra(Intent.EXTRA_TEXT, msg)
                    sendIntent.putExtra(
                        "jid", title // the (Whatsapp) phone number of contact
                    )
                    sendIntent.setPackage("com.whatsapp")
                    //  sendIntent.putExtra("jid", recipient+"@s.whatsapp.net")
                    sendIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                    startActivity(sendIntent)

                  */
                }
            "com.google.android.dialer" -> {
                if (text == "Incoming call") {
                    Toast.makeText(context, "$text from $title", Toast.LENGTH_SHORT).show()

                    val number: String = title.toString()
                    val msg = "text to send"
                    try {
                        val smsManager: SmsManager = getSystemService(SmsManager::class.java)
                        /*      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                  getSystemService(SmsManager::class.java)
                              } else {
                                  SmsManager.getDefault()
                              } */
                        smsManager.sendTextMessage(number, null, msg, null, null)
                        Toast.makeText(applicationContext, "Message Sent", Toast.LENGTH_LONG).show()
                    } catch (e: Exception) {
                        Toast.makeText(applicationContext, "Some thing is wrong", Toast.LENGTH_LONG)
                            .show()
                    }
                }
            }
            "com.google.android.apps.messaging" -> {
                Toast.makeText(context, "SMS from $title", Toast.LENGTH_SHORT).show()
                val number: String = title.toString()
                val msg = "text to send"

                val smsIntent = Intent(Intent.ACTION_SENDTO)
                smsIntent.data = Uri.parse(number)
                smsIntent.putExtra("sms_body", msg)
                if (smsIntent.resolveActivity(packageManager) != null) {
                    startActivity(smsIntent)
                    Toast.makeText(context, "SMS sent back", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(context, "something is wrong", Toast.LENGTH_SHORT).show()
                }
            }
            else -> Unit //Toast.makeText(context, "something else", Toast.LENGTH_SHORT).show()
        }

        if (id != null) {
            val stream = ByteArrayOutputStream()
            // id.compress(Bitmap.CompressFormat.PNG, 100, stream);
            val byteArray = stream.toByteArray()
            msgInfo.putExtra("icon", byteArray)
        }
    }

    override fun onNotificationRemoved(sbn: StatusBarNotification) {
        super.onNotificationRemoved(sbn)
    }
}

并且:

// AndroidManifest.xml

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

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

    <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/Theme.Notification">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.Notification">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <service android:name=".NotificationListener"
            android:label="@string/service_name"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
            android:exported="false">
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>

    </application>

</manifest>

我从here了解到,这个问题可能是由于you have opened something but never close them,但我找不到这个东西!

j9per5c4

j9per5c41#

我遇到了同样的问题,我的问题解决如下,帮助-检查更新

相关问题