{"id":257,"date":"2022-02-21T13:33:10","date_gmt":"2022-02-21T05:33:10","guid":{"rendered":"http:\/\/blog.iichen.cn\/?p=257"},"modified":"2022-02-21T13:33:10","modified_gmt":"2022-02-21T05:33:10","slug":"android-%e9%80%9a%e7%9f%a5%e6%a0%8f","status":"publish","type":"post","link":"https:\/\/iichen.cn\/?p=257","title":{"rendered":"Android-\u901a\u77e5\u680f\u76d1\u542c"},"content":{"rendered":"<h3>\u4e00\u3001\u5224\u65ad\u5e94\u7528\u662f\u5426\u5305\u542b\u76d1\u542c\u6743\u9650<\/h3>\n<pre><code class=\"language-java\">private fun isEnabled(): Boolean {\n    val pkgName = packageName\n    val flat: String =\n        Settings.Secure.getString(contentResolver, \"enabled_notification_listeners\")\n    if (!TextUtils.isEmpty(flat)) {\n        val names = flat.split(\":\".toRegex()).toTypedArray()\n        for (i in names.indices) {\n            val cn = ComponentName.unflattenFromString(names[i])\n            if (cn != null) {\n                if (TextUtils.equals(pkgName, cn.packageName)) {\n                    return true\n                }\n            }\n        }\n    }\n    return false\n}<\/code><\/pre>\n<h3>\u4e8c\u3001\u5f00\u542f\u76d1\u542c\u548c\u8df3\u8f6c<\/h3>\n<pre><code class=\"language-java\">if (!isEnabled()) {\n    startActivity(Intent(\"android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS\"))\n} else {\n    val toast =\n        Toast.makeText(applicationContext, \"\u76d1\u63a7\u5668\u5f00\u5173\u5df2\u6253\u5f00\", Toast.LENGTH_SHORT)\n    toast.show()\n}\n\nif (isEnabled()) {\n    startActivity(Intent(\"android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS\"))\n} else {\n    val toast =\n        Toast.makeText(applicationContext, \"\u76d1\u63a7\u5668\u5f00\u5173\u5df2\u5173\u95ed\", Toast.LENGTH_SHORT)\n    toast.show()\n}\n<\/code><\/pre>\n<h3>\u4e09\u3001\u5b9a\u4e49\u76d1\u542c\u670d\u52a1<\/h3>\n<pre><code class=\"language-java\">@SuppressLint(\"NewApi\")\nclass NotificationMonitorService : NotificationListenerService() {\n    \/\/ \u5728\u6536\u5230\u6d88\u606f\u65f6\u89e6\u53d1\n    override fun onNotificationPosted(sbn: StatusBarNotification) {\n        \/\/ \u83b7\u53d6\u6807\u51c6\u6d88\u606f\u5185\u5bb9\n        val extras = sbn.notification.extras\n        \/\/ \u83b7\u53d6\u63a5\u6536\u6d88\u606fAPP\u7684\u5305\u540d\n        val notificationPkg = sbn.packageName\n        \/\/ \u83b7\u53d6\u63a5\u6536\u6d88\u606f\u7684\u62ac\u5934\n        val notificationTitle =\n            extras.getString(Notification.EXTRA_TITLE)\n\n        val tickerText = sbn.notification.tickerText\n        \/\/ \u83b7\u53d6\u63a5\u6536\u6d88\u606f\u7684\u5185\u5bb9\n        val notificationText = extras.getString(Notification.EXTRA_TEXT)\n        Log.i(\n            \"iichen\",\n            \"Notification \u6807\u51c6 posted $notificationTitle & $notificationText & $tickerText & $notificationPkg\"\n        )\n\n        \/\/ \u83b7\u53d6\u975e\u6807\u51c6\u901a\u77e5\u5185\u5bb9\n        val view = getContentView(this,sbn.notification)\n        view?.apply {\n            val content = getContent(this)\n            Log.i(\n                \"iichen\",\n                \"Notification \u975e\u6807\u51c6 posted $notificationTitle & $content\"\n            )\n        }\n\n    }\n\n    \/\/ \u5728\u5220\u9664\u6d88\u606f\u65f6\u89e6\u53d1\n    override fun onNotificationRemoved(sbn: StatusBarNotification) {\n        \/\/ TODO Auto-generated method stub\n        val extras = sbn.notification.extras\n        \/\/ \u83b7\u53d6\u63a5\u6536\u6d88\u606fAPP\u7684\u5305\u540d\n        val notificationPkg = sbn.packageName\n        \/\/ \u83b7\u53d6\u63a5\u6536\u6d88\u606f\u7684\u62ac\u5934\n        val notificationTitle =\n            extras.getString(Notification.EXTRA_TITLE)\n        \/\/ \u83b7\u53d6\u63a5\u6536\u6d88\u606f\u7684\u5185\u5bb9\n        val notificationText = extras.getString(Notification.EXTRA_TEXT)\n        Log.i(\n            \"iichen\",\n            \"Notification removed $notificationTitle & $notificationText\"\n        )\n    }\n\n    \/\/\u83b7\u53d6notification\u7684view\n    fun getContentView(context: Context?, notification: Notification): View? {\n        var contentView: RemoteViews? = null\n        \/\/\u83b7\u53d6contentView\n        if (notification.contentView != null) {\n            contentView = notification.contentView\n        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {\n            contentView = Notification.Builder.recoverBuilder(context, notification)\n                .createContentView()\n        }\n\n        \/\/RemoteViews\u8f6c\u6210view\n        var view: View? = null\n        try {\n            view = contentView?.apply(context, null)\n        } catch (e: Throwable) {\n\n        }\n        return view\n    }\n\n    \/\/\u83b7\u53d6view\u91cc\u9762\u7684\u6587\u672c\n    fun getContent(view: View): String? {\n        val stringBuilder = StringBuilder()\n        traversalView(view, stringBuilder)\n        return stringBuilder.toString()\n    }\n\n    \/\/\u904d\u5386View\uff0c\u83b7\u53d6TextView\u91cc\u9762\u7684\u6587\u672c\u5185\u5bb9\n    private fun traversalView(\n        view: View,\n        stringBuilder: StringBuilder\n    ) {\n        if (view is ViewGroup) {\n            val viewGroup = view as ViewGroup\n            val count = viewGroup.childCount\n            for (i in 0 until count) {\n                val childView: View = viewGroup.getChildAt(i)\n                traversalView(childView, stringBuilder)\n            }\n        } else {\n            if (view is TextView) {\n                val tv = view as TextView\n                val text = tv.text\n                stringBuilder.append(text)\n                stringBuilder.append(\";\")\n            }\n        }\n    }\n\n}\n<\/code><\/pre>\n<h3>\u56db\u3001\u6e05\u5355\u6587\u4ef6\u914d\u7f6e<\/h3>\n<pre><code class=\"language-java\"><service android:name=\".service.NotificationMonitorService\"\n    android:label=\"NotificationMonitor\"\n    android:permission=\"android.permission.BIND_NOTIFICATION_LISTENER_SERVICE\">\n    <intent-filter>\n        <action android:name=\"android.service.notification.NotificationListenerService\" \/>\n    <\/intent-filter>\n<\/service><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5224\u65ad\u5e94\u7528\u662f\u5426\u5305\u542b\u76d1\u542c\u6743\u9650 private fun isEnabled(): Boolean { val p [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,8],"tags":[],"class_list":["post-257","post","type-post","status-publish","format-standard","hentry","category-android","category-8"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android-\u901a\u77e5\u680f\u76d1\u542c - IIchen<\/title>\n<meta name=\"description\" content=\"Android\u901a\u77e5\u680f\u76d1\u542c-\u666e\u901a\u5185\u5bb9\u83b7\u53d6\u4ee5\u53ca\u9690\u85cfView\u5185\u5bb9\u83b7\u53d6\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/iichen.cn\/?p=257\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android-\u901a\u77e5\u680f\u76d1\u542c - IIchen\" \/>\n<meta property=\"og:description\" content=\"Android\u901a\u77e5\u680f\u76d1\u542c-\u666e\u901a\u5185\u5bb9\u83b7\u53d6\u4ee5\u53ca\u9690\u85cfView\u5185\u5bb9\u83b7\u53d6\" \/>\n<meta property=\"og:url\" content=\"https:\/\/iichen.cn\/?p=257\" \/>\n<meta property=\"og:site_name\" content=\"IIchen\" \/>\n<meta property=\"article:published_time\" content=\"2022-02-21T05:33:10+00:00\" \/>\n<meta name=\"author\" content=\"iichen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"iichen\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/iichen.cn\/?p=257#article\",\"isPartOf\":{\"@id\":\"https:\/\/iichen.cn\/?p=257\"},\"author\":{\"name\":\"iichen\",\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\"},\"headline\":\"Android-\u901a\u77e5\u680f\u76d1\u542c\",\"datePublished\":\"2022-02-21T05:33:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/iichen.cn\/?p=257\"},\"wordCount\":1,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\"},\"articleSection\":[\"Android\",\"\u7b14\u8bb0\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/iichen.cn\/?p=257#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/iichen.cn\/?p=257\",\"url\":\"https:\/\/iichen.cn\/?p=257\",\"name\":\"Android-\u901a\u77e5\u680f\u76d1\u542c - IIchen\",\"isPartOf\":{\"@id\":\"https:\/\/iichen.cn\/#website\"},\"datePublished\":\"2022-02-21T05:33:10+00:00\",\"description\":\"Android\u901a\u77e5\u680f\u76d1\u542c-\u666e\u901a\u5185\u5bb9\u83b7\u53d6\u4ee5\u53ca\u9690\u85cfView\u5185\u5bb9\u83b7\u53d6\",\"breadcrumb\":{\"@id\":\"https:\/\/iichen.cn\/?p=257#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/iichen.cn\/?p=257\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/iichen.cn\/?p=257#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/iichen.cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android-\u901a\u77e5\u680f\u76d1\u542c\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/iichen.cn\/#website\",\"url\":\"https:\/\/iichen.cn\/\",\"name\":\"IIchen\",\"description\":\"Just do it!\",\"publisher\":{\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/iichen.cn\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\",\"name\":\"iichen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg\",\"contentUrl\":\"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg\",\"width\":940,\"height\":940,\"caption\":\"iichen\"},\"logo\":{\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/www.iichen.cn\"],\"url\":\"https:\/\/iichen.cn\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android-\u901a\u77e5\u680f\u76d1\u542c - IIchen","description":"Android\u901a\u77e5\u680f\u76d1\u542c-\u666e\u901a\u5185\u5bb9\u83b7\u53d6\u4ee5\u53ca\u9690\u85cfView\u5185\u5bb9\u83b7\u53d6","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/iichen.cn\/?p=257","og_locale":"zh_CN","og_type":"article","og_title":"Android-\u901a\u77e5\u680f\u76d1\u542c - IIchen","og_description":"Android\u901a\u77e5\u680f\u76d1\u542c-\u666e\u901a\u5185\u5bb9\u83b7\u53d6\u4ee5\u53ca\u9690\u85cfView\u5185\u5bb9\u83b7\u53d6","og_url":"https:\/\/iichen.cn\/?p=257","og_site_name":"IIchen","article_published_time":"2022-02-21T05:33:10+00:00","author":"iichen","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"iichen","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/iichen.cn\/?p=257#article","isPartOf":{"@id":"https:\/\/iichen.cn\/?p=257"},"author":{"name":"iichen","@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c"},"headline":"Android-\u901a\u77e5\u680f\u76d1\u542c","datePublished":"2022-02-21T05:33:10+00:00","mainEntityOfPage":{"@id":"https:\/\/iichen.cn\/?p=257"},"wordCount":1,"commentCount":0,"publisher":{"@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c"},"articleSection":["Android","\u7b14\u8bb0"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/iichen.cn\/?p=257#respond"]}]},{"@type":"WebPage","@id":"https:\/\/iichen.cn\/?p=257","url":"https:\/\/iichen.cn\/?p=257","name":"Android-\u901a\u77e5\u680f\u76d1\u542c - IIchen","isPartOf":{"@id":"https:\/\/iichen.cn\/#website"},"datePublished":"2022-02-21T05:33:10+00:00","description":"Android\u901a\u77e5\u680f\u76d1\u542c-\u666e\u901a\u5185\u5bb9\u83b7\u53d6\u4ee5\u53ca\u9690\u85cfView\u5185\u5bb9\u83b7\u53d6","breadcrumb":{"@id":"https:\/\/iichen.cn\/?p=257#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/iichen.cn\/?p=257"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/iichen.cn\/?p=257#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/iichen.cn\/"},{"@type":"ListItem","position":2,"name":"Android-\u901a\u77e5\u680f\u76d1\u542c"}]},{"@type":"WebSite","@id":"https:\/\/iichen.cn\/#website","url":"https:\/\/iichen.cn\/","name":"IIchen","description":"Just do it!","publisher":{"@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/iichen.cn\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c","name":"iichen","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/iichen.cn\/#\/schema\/person\/image\/","url":"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg","contentUrl":"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg","width":940,"height":940,"caption":"iichen"},"logo":{"@id":"https:\/\/iichen.cn\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/www.iichen.cn"],"url":"https:\/\/iichen.cn\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/posts\/257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=257"}],"version-history":[{"count":0,"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/posts\/257\/revisions"}],"wp:attachment":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}