{"id":470,"date":"2011-04-11T07:51:31","date_gmt":"2011-04-10T23:51:31","guid":{"rendered":"http:\/\/leybreeze.com\/blog\/?p=470"},"modified":"2011-04-23T12:03:22","modified_gmt":"2011-04-23T04:03:22","slug":"%e5%88%9b%e5%bb%ba%e9%80%9a%e7%9f%a5%e6%a0%8f%e9%80%9a%e7%9f%a5","status":"publish","type":"post","link":"http:\/\/leybreeze.com\/blog\/?p=470","title":{"rendered":"\u521b\u5efa\u72b6\u6001\u680f\u901a\u77e5"},"content":{"rendered":"<p>\u72b6\u6001\u680f\u901a\u77e5\u4f1a\u5411\u7cfb\u7edf\u72b6\u6001\u680f\u6dfb\u52a0\u4e00\u4e2a\u56fe\u6807\uff08\u8fd8\u53ef\u4ee5\u9644\u6709\u6587\u672c\u6d88\u606f\uff09\u4ee5\u53ca\u4e00\u6761\u201c\u901a\u77e5\u201d\u7a97\u53e3\u5185\u7684\u53ef\u6269\u5c55\u6d88\u606f\u3002\u5f53\u7528\u6237\u9009\u62e9\u4e86\u8be5\u53ef\u6269\u5c55\u6d88\u606f\uff0cAndroid\u53d1\u51fa\u4e00\u4e2a\u6709\u901a\u77e5\u5b9a\u4e49\u7684Intent\uff08\u901a\u5e38\u6765\u542f\u52a8\u4e00\u4e2a\u6d3b\u52a8\uff09\u3002\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u901a\u77e5\u6765\u4ee5\u58f0\u97f3\u3001\u632f\u52a8\u548c\u8bbe\u5907\u95ea\u5149\u6765\u63d0\u9192\u7528\u6237\u3002<\/p>\n<p>\u72b6\u6001\u680f\u901a\u77e5\u5e94\u5f53\u88ab\u7528\u4e8e\u4efb\u4f55\u540e\u53f0\u670d\u52a1\u9700\u8981\u63d0\u793a\u7528\u6237\u53d1\u751f\u4e86\u4e8b\u4ef6\u5e76\u8981\u6c42\u56de\u5e94\u7684\u60c5\u51b5\u3002\u540e\u53f0\u670d\u52a1\u4e0d\u5e94\u81ea\u5df1\u542f\u52a8\u6d3b\u52a8\u6765\u63a5\u6536\u7528\u6237\u4ea4\u4e92\u64cd\u4f5c\u3002\u5b83\u5e94\u5f53\u521b\u5efa\u4e00\u4e2a\u72b6\u6001\u680f\u901a\u77e5\uff0c\u5728\u7528\u6237\u9009\u62e9\u8be5\u901a\u77e5\u65f6\u542f\u52a8\u4e00\u4e2a\u6d3b\u52a8\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u622a\u56fe\u5c55\u793a\u4e86\u4e00\u4e2a\u5de6\u4fa7\u6709\u901a\u77e5\u56fe\u6807\u7684\u72b6\u6001\u680f\u3002<\/p>\n<p><a href=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/04\/status_bar.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-471\" title=\"status_bar\" src=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/04\/status_bar.png\" alt=\"\" width=\"200\" height=\"25\" \/><\/a>\u4e0b\u4e00\u5f20\u622a\u56fe\u5c55\u793a\u4e86\u201c\u901a\u77e5\u201d\u7a97\u53e3\u4e2d\u6269\u5c55\u7684\u901a\u77e5\u6d88\u606f\u3002\u5c06\u72b6\u6001\u680f\u4e0b\u62c9\u53ef\u5c55\u5f00\u901a\u77e5\u7a97\u53e3\uff08\u6216\u4ece\u4e3b\u9875\u9762\u9009\u9879\u83dc\u5355\u4e2d\u9009\u62e9\u201c\u901a\u77e5\u201d\uff09\u3002<\/p>\n<h2><a href=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/04\/notifications_window.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-476\" title=\"notifications_window\" src=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/04\/notifications_window.png\" alt=\"\" width=\"200\" height=\"157\" \/><\/a>\u57fa\u672c\u5185\u5bb9<\/h2>\n<p>\u6d3b\u52a8\u6216\u662f\u670d\u52a1\u53ef\u4ee5\u521d\u59cb\u5316\u4e00\u4e2a\u72b6\u6001\u680f\u901a\u77e5\u3002\u7531\u4e8e\u4e00\u4e2a\u6d3b\u52a8\u53ea\u6709\u5728\u5176\u72b6\u6001\u662f\u6d3b\u52a8\u7684\u4e14\u5904\u4e8e\u7126\u70b9\u65f6\u624d\u80fd\u6267\u884c\u884c\u52a8\uff0c\u6240\u4ee5\u5e94\u5f53\u901a\u8fc7\u670d\u52a1\u6765\u521b\u5efa\u72b6\u6001\u680f\u901a\u77e5\u3002\u8fd9\u6837\uff0c\u901a\u77e5\u5c31\u80fd\u4ece\u540e\u53f0\u521b\u5efa\uff0c\u5373\u4f7f\u7528\u6237\u5728\u4f7f\u7528\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u6216\u662f\u8bbe\u5907\u5904\u4e8e\u4f11\u7720\u72b6\u6001\u3002\u8981\u521b\u5efa\u901a\u77e5\uff0c\u5fc5\u987b\u4f7f\u7528\u4e24\u4e2a\u7c7b\uff1aNotification\u548cNotificationManager\u3002<\/p>\n<p>\u4f7f\u7528Notification\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\u6765\u5b9a\u4e49\u72b6\u6001\u680f\u901a\u77e5\u7684\u5c5e\u6027\uff0c\u4f8b\u5982\u72b6\u6001\u680f\u56fe\u6807\uff0c\u6269\u5c55\u6d88\u606f\uff0c\u6216\u8981\u64ad\u653e\u7684\u97f3\u6548\u7b49\u66f4\u591a\u5185\u5bb9\u3002NotificationManager\u662f\u4e00\u4e2aAndroid\u7cfb\u7edf\u670d\u52a1\uff0c\u5b83\u6267\u884c\u5e76\u7ba1\u7406\u6240\u6709Notification\u3002\u4e0d\u5fc5\u5b9e\u4f8b\u5316NotificationManager\u3002\u4e3a\u4e86\u4f20\u9012Notification\uff0c\u5fc5\u987b\u901a\u8fc7getSystemService()\u83b7\u53d6NotificationManager\u7684\u4e00\u4e2a\u5f15\u7528\uff0c\u4e4b\u540e\u518d\u5e0c\u671b\u901a\u77e5\u7528\u6237\u65f6\uff0c\u4f7f\u7528notify()\u5c06\u5176\u4f20\u9012\u7ed9\u81ea\u5df1\u7684Notification\u5bf9\u8c61\u3002<\/p>\n<p>\u8981\u521b\u5efa\u4e00\u4e2a\u72b6\u6001\u680f\u901a\u77e5\uff1a<\/p>\n<p>1.\u83b7\u53d6NotificationManager\u7684\u4e00\u4e2a\u5f15\u7528<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #800080;\">String<\/span> ns = <span style=\"color: #800080;\">Context<\/span>.NOTIFICATION_SERVICE;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">NotificationManager<\/span> mNotificationManager = (<span style=\"color: #800080;\">NotificationManager<\/span>) getSystemService(ns);<\/span><\/pre>\n<p>2.\u5b9e\u4f8b\u5316Notification\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #000080;\">int<\/span> icon = R.drawable.notification_icon;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">CharSequence<\/span> tickerText = <span style=\"color: #008000;\">\"Hello\"<\/span>;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">long when<\/span> = <span style=\"color: #800080;\">System<\/span>.currentTimeMillis();<\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">Notification<\/span> notification = <span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">Notification<\/span>(icon, tickerText, <span style=\"color: #000080;\">when<\/span>);<\/span><\/pre>\n<p>3.\u5b9a\u4e49Notification\u7684\u6269\u5c55\u6d88\u606f\u548cIntent\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #800080;\">Context<\/span> context = getApplicationContext();<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">CharSequence<\/span> contentTitle = <span style=\"color: #008000;\">\"My notification\"<\/span>;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">CharSequence<\/span> contentText = <span style=\"color: #008000;\">\"Hello World!\"<\/span>;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">Intent<\/span> notificationIntent = <span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">Intent<\/span>(<span style=\"color: #000080;\">this<\/span>, <span style=\"color: #800080;\">MyClass<\/span>.<span style=\"color: #000080;\">class<\/span>);<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">PendingIntent<\/span> contentIntent = <span style=\"color: #800080;\">PendingIntent<\/span>.getActivity(<span style=\"color: #000080;\">this<\/span>, 0, notificationIntent, 0);<\/span>\r\n\r\n<span style=\"color: #000000;\">notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);<\/span><\/pre>\n<p>4.\u5c06Notification\u4f20\u9012\u7ed9NotificationManager\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #000080;\">private static final int<\/span> HELLO_ID = 1;<\/span>\r\n\r\n<span style=\"color: #000000;\">mNotificationManager.notify(HELLO_ID, notification);<\/span><\/pre>\n<p>\u8fd9\u6837\u4e4b\u540e\uff0c\u7528\u6237\u5c06\u6536\u5230\u901a\u77e5\u3002<\/p>\n<h2>\u7ba1\u7406\u901a\u77e5<\/h2>\n<p>\u4e00\u4e2aNotification\u5bf9\u8c61\u5b9a\u4e49\u663e\u793a\u4e8e\u72b6\u6001\u680f\u548c\u901a\u77e5\u7a97\u53e3\u7684\u901a\u77e5\u6d88\u606f\u7684\u5177\u4f53\u7ec6\u8282\u4ee5\u53ca\u5176\u4ed6\u63d0\u793a\u8bbe\u7f6e\uff0c\u4f8b\u5982\u58f0\u97f3\u6216\u662f\u706f\u5149\u95ea\u70c1\u3002\u5fc5\u987b\u901a\u8fc7getSystemService()\u65b9\u6cd5\u68c0\u7d22\u4e00\u4e2a\u6307\u5411\u5b83\u7684\u5f15\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #800080;\">String <\/span>ns = <span style=\"color: #800080;\">Context<\/span>.NOTIFICATION_SERVICE;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">NotificationManager<\/span> mNotificationManager = (<span style=\"color: #800080;\">NotificationManager<\/span>) getSystemService(ns);<\/span><\/pre>\n<p>\u5728\u5e0c\u671b\u53d1\u9001\u72b6\u6001\u680f\u901a\u77e5\u65f6\uff0c\u9700\u8981\u7528notify(int, Notification)\u6765\u4f20\u9012Notification\u5bf9\u8c61\u81f3NotificationManager\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570\u662fNotification\u7684\u552f\u4e00ID\uff0c\u7b2c\u4e8c\u4e2a\u662fNotification\u5bf9\u8c61\u3002ID\u552f\u4e00\u8bc6\u522b\u4e86\u7a0b\u5e8f\u4e2d\u7684Notification\u3002\u5982\u679c\u9700\u8981\u66f4\u65b0Notification\u6216\uff08\u7a0b\u5e8f\u7ba1\u7406\u591a\u4e2a\u4e0d\u540cNotification\u65f6\uff09\u5728\u7528\u6237\u7531Notification\u4e2d\u5b9a\u4e49\u7684Intent\u8fd4\u56de\u7a0b\u5e8f\u65f6\u9009\u62e9\u6b63\u786e\u7684\u884c\u52a8\uff0c\u8fd9\u5c06\u4f1a\u662f\u5f88\u5fc5\u8981\u7684\u3002<\/p>\n<p>\u8981\u5728\u7528\u6237\u4ece\u901a\u77e5\u7a97\u53e3\u4e2d\u9009\u62e9\u72b6\u6001\u680f\u901a\u77e5\u7684\u540c\u65f6\u6e05\u9664\u5b83\u7684\u8bdd\uff0c\u5c31\u5411Notification\u5bf9\u8c61\u6dfb\u52a0&#8221;FLAG_AUTO_CANCEL&#8221;\u65d7\u6807\u3002\u4e5f\u53ef\u4ee5\u624b\u52a8\u5730\u901a\u8fc7\u5411cancel(int)\u4f20\u9012\u901a\u77e5ID\u6765\u6e05\u9664\uff0c\u6216\u4f7f\u7528cancelAll()\u6765\u6e05\u9664\u5168\u90e8\u901a\u77e5\u3002<\/p>\n<h2>\u521b\u5efa\u901a\u77e5<\/h2>\n<p>\u4e00\u4e2aNotification\u5bf9\u8c61\u5b9a\u4e49\u4e86\u663e\u793a\u5728\u72b6\u6001\u680f\u548c\u901a\u77e5\u7a97\u53e3\u7684\u901a\u77e5\u6d88\u606f\u7684\u7ec6\u8282\uff0c\u4ee5\u53ca\u5176\u4ed6\u63d0\u793a\u7684\u8bbe\u7f6e\uff0c\u4f8b\u5982\u58f0\u97f3\u548c\u706f\u5149\u95ea\u70c1\u3002<\/p>\n<p>\u4e00\u4e2a\u72b6\u6001\u680f\u901a\u77e5\u9700\u8981\u4ee5\u4e0b\u6240\u6709\u5185\u5bb9\uff1a<\/p>\n<ul>\n<li>\u4e00\u4e2a\u72b6\u6001\u680f\u7684\u56fe\u6807<\/li>\n<li>\u4e00\u4e2a\u6807\u9898\u548c\u6269\u5c55\u89c6\u56fe\u4e2d\u7684\u6269\u5c55\u6d88\u606f\uff08\u9664\u975e\u53e6\u5916\u81ea\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7684\u6269\u5c55\u89c6\u56fe\uff09<\/li>\n<li>\u4e00\u4e2aPendingIntent\uff0c\u4ee5\u4f9b\u5728\u901a\u77e5\u88ab\u9009\u4e2d\u65f6\u653e\u51fa<\/li>\n<\/ul>\n<p>\u72b6\u6001\u680f\u901a\u77e5\u7684\u53ef\u9009\u8bbe\u7f6e\u5219\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u72b6\u6001\u680f\u7684ticker-text\u6d88\u606f\uff08\u6ce8\uff1a\u6682\u65f6\u8fd8\u6ca1\u6709\u51b3\u5b9a\u600e\u4e48\u7ffb\u8bd1\u8fd9\u4e2aticker-text\u6bd4\u8f83\u597d\uff09<\/li>\n<li>\u63d0\u793a\u97f3<\/li>\n<li>\u632f\u52a8\u8bbe\u7f6e<\/li>\n<li>LED\u706f\u8bbe\u7f6e<\/li>\n<\/ul>\n<p>\u65b0\u5efa\u4e00\u4e2aNotification\u7684\u57fa\u672c\u5185\u5bb9\u5305\u62ec\u6784\u9020\u51fd\u6570Notification(int, CharSequence, long)\u548csetLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent)\u65b9\u6cd5\u3002\u5b83\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2aNotification\u6240\u9700\u7684\u6240\u6709\u8bbe\u7f6e\u3002\u4e0b\u9762\u7684\u7247\u6bb5\u5c55\u793a\u4e86\u4e00\u79cd\u57fa\u672c\u7684\u901a\u77e5\u521b\u5efa\u65b9\u5f0f\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #000080;\">int<\/span> icon = R.drawable.notification_icon; \u00a0 \u00a0 \u00a0 \u00a0<span style=\"color: #993300;\">\/\/ icon from resources<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">CharSequence<\/span> tickerText = <span style=\"color: #008000;\">\"Hello\"<\/span>; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span style=\"color: #993300;\">\/\/ ticker-text<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">long when<\/span> =<span style=\"color: #800080;\"> System<\/span>.currentTimeMillis(); \u00a0 \u00a0 \u00a0 \u00a0 <span style=\"color: #993300;\">\/\/ notification time<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">Context<\/span> context = getApplicationContext(); \u00a0 \u00a0 <span style=\"color: #993300;\">\u00a0\/\/ application Context<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">CharSequence<\/span> contentTitle = <span style=\"color: #008000;\">\"My notification\"<\/span>; \u00a0<span style=\"color: #993300;\">\/\/ expanded message title<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">CharSequence<\/span> contentText = <span style=\"color: #008000;\">\"Hello World!\"<\/span>; \u00a0 \u00a0 <span style=\"color: #993300;\">\u00a0\/\/ expanded message text<\/span><\/span>\r\n\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">Intent<\/span> notificationIntent = <span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">Intent<\/span>(<span style=\"color: #000080;\">this<\/span>, MyClass.<span style=\"color: #000080;\">class<\/span>);<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">PendingIntent<\/span> contentIntent = <span style=\"color: #800080;\">PendingIntent<\/span>.getActivity(<span style=\"color: #000080;\">this<\/span>, 0, notificationIntent, 0);<\/span>\r\n\r\n<span style=\"color: #993300;\">\/\/ the next two lines initialize the Notification, using the configurations above<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">Notification<\/span> notification = <span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">Notification<\/span>(icon, tickerText, <span style=\"color: #000080;\">when<\/span>);<\/span>\r\n<span style=\"color: #000000;\">notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);<\/span><\/pre>\n<h2>\u66f4\u65b0\u901a\u77e5<\/h2>\n<p>\u5728\u7a0b\u5e8f\u4e2d\u7684\u4e8b\u4ef6\u6301\u7eed\u53d1\u5c55\u7684\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u4e0d\u65ad\u66f4\u65b0\u72b6\u6001\u680f\u901a\u77e5\u4e2d\u7684\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u5728\u524d\u4e00\u6761\u77ed\u4fe1\u88ab\u9605\u8bfb\u524d\u53c8\u6536\u5230\u65b0\u7684\u4e00\u6761\u65f6\uff0c\u77ed\u6d88\u606f\u7a0b\u5e8f\u4f1a\u66f4\u65b0\u73b0\u6709\u901a\u77e5\uff0c\u663e\u793a\u6536\u5230\u7684\u672a\u8bfb\u77ed\u4fe1\u7684\u603b\u6570\u3002\u8fd9\u4e00\u66f4\u65b0\u901a\u77e5\u7684\u505a\u6cd5\u8981\u6bd4\u5411NotificationManager\u6dfb\u52a0\u65b0\u7684\u901a\u77e5\u66f4\u597d\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u4f7f\u901a\u77e5\u7a97\u53e3\u53d8\u5f97\u6742\u4e71\u3002<\/p>\n<p>\u7531\u4e8e\u6bcf\u4e00\u4e2a\u901a\u77e5\u90fd\u6709NotificationManager\u901a\u8fc7\u4e00\u4e2a\u6574\u578bID\u552f\u4e00\u5b9a\u4e49\uff0c\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7\u4ee5\u65b0\u503c\u8c03\u7528setLatestEventInfo()\u6765\u4fee\u6539\u901a\u77e5\uff0c\u6539\u53d8Notification\u7684\u4e00\u4e9b\u57df\u7684\u503c\uff0c\u4e4b\u540e\u518d\u6b21\u8c03\u7528notify()\u3002<\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u6210\u5458\u57df\u6765\u4fee\u6539\u6bcf\u4e00\u79cd\u5c5e\u6027\uff08\u9664\u4e86Context\u548c\u6269\u5c55\u6d88\u606f\u6807\u9898\u53ca\u6587\u672c\uff09\u3002\u5e94\u5f53\u59cb\u7ec8\u5728\u901a\u8fc7\u65b0\u503c\u8c03\u7528setLatestEventInfo()\u66f4\u65b0\u901a\u77e5\u65f6\u4fee\u6539<em><span style=\"color: #008000;\">contentTitle<\/span><\/em>\u548c<em><span style=\"color: #008000;\">contentText<\/span><\/em>\u7684\u6587\u672c\u6d88\u606f\u3002\u4e4b\u540e\u8c03\u7528notify()\u6765\u66f4\u65b0\u901a\u77e5\u3002\uff08\u5f53\u7136\uff0c\u5982\u679c\u521b\u5efa\u4e86\u81ea\u5b9a\u4e49\u6269\u5c55\u89c6\u56fe\uff0c\u90a3\u66f4\u65b0\u8fd9\u4e9b\u6807\u9898\u548c\u6587\u672c\u7684\u503c\u5c31\u6ca1\u6709\u6548\u679c\u4e86\u3002\uff09<\/p>\n<h2>\u6dfb\u52a0\u58f0\u97f3<\/h2>\n<p>\u53ef\u4ee5\u4f7f\u7528\uff08\u7528\u6237\u6240\u5b9a\u4e49\u7684\uff09\u9ed8\u8ba4\u6d88\u606f\u63d0\u793a\u97f3\u6216\u662f\u7a0b\u5e8f\u6307\u5b9a\u7684\u58f0\u97f3\u6765\u63d0\u793a\u7528\u6237\u3002<\/p>\n<p>\u8981\u4f7f\u7528\u7528\u6237\u9ed8\u8ba4\u63d0\u793a\u97f3\uff0c\u5728<em><span style=\"color: #008000;\">defaults<\/span><\/em>\u57df\u4e2d\u6dfb\u52a0\u201cDEFAULT_SOUND\u201d\uff1a<\/p>\n<pre><span style=\"color: #000000;\">notification.defaults |= <span style=\"color: #800080;\">Notification<\/span>.DEFAULT_SOUND;<\/span><\/pre>\n<p>\u8981\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u63d0\u793a\u97f3\uff0c\u9700\u8981\u4f20\u5165\u4e00\u4e2aUri\u5f15\u7528\u81f3<em><span style=\"color: #008000;\">sound<\/span><\/em>\u57df\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u4f7f\u7528\u4e86\u4e00\u4e2a\u4fdd\u5b58\u4e8e\u8bbe\u5907SD\u5361\u4e2d\u7684\u5df2\u77e5\u97f3\u9891\u6587\u4ef6\uff1a<\/p>\n<pre><span style=\"color: #000000;\">notification.sound = <span style=\"color: #800080;\">Uri<\/span>.parse(<span style=\"color: #008000;\">\"file:\/\/\/sdcard\/notification\/ringer.mp3\"<\/span>);<\/span><\/pre>\n<p>\u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4e2d\uff0c\u97f3\u9891\u6587\u4ef6\u662f\u4ece\u5185\u90e8\u7684MediaStore\u7684ContentProvider\u4e2d\u9009\u62e9\u7684\uff1a<\/p>\n<pre><span style=\"color: #000000;\">notification.sound = <span style=\"color: #800080;\">Uri<\/span>.withAppendedPath(<span style=\"color: #800080;\">Audio.Media.<\/span>INTERNAL_CONTENT_URI, <span style=\"color: #008000;\">\"6\"<\/span>);<\/span><\/pre>\n<p>\u5728\u672c\u4f8b\u4e2d\uff0c\u5a92\u4f53\u6587\u4ef6\u7684\u786e\u5207ID\uff08&#8221;6&#8243;\uff09\u662f\u5df2\u77e5\u7684\uff0c\u9644\u52a0\u4e8e\u5185\u5bb9Uri\u4e4b\u4e0a\u3002\u5982\u679c\u4e0d\u77e5\u9053\u786e\u5207\u7684ID\uff0c\u5c31\u5fc5\u987b\u4f7f\u7528ContentResolver\u6765\u8bf7\u6c42\u6240\u6709MediaStore\u5185\u53ef\u7528\u7684\u5a92\u4f53\u3002\u53c2\u89c1\u5185\u5bb9\u63d0\u4f9b\u8005\u6587\u6863\u4e2d\u5173\u4e8e\u4f7f\u7528ContentResolver\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<p>\u5982\u679c\u5e0c\u671b\u58f0\u97f3\u5728\u7528\u6237\u54cd\u5e94\u901a\u77e5\u6216\u901a\u77e5\u88ab\u53d6\u6d88\u524d\u4e0d\u65ad\u6301\u7eed\uff0c\u5c31\u5728<em><span style=\"color: #008000;\">flags<\/span><\/em>\u57df\u4e2d\u6dfb\u52a0\u201cFLAG_INSISTENT\u201d\u3002<\/p>\n<p style=\"padding-left: 30px;\"><strong>\u6ce8\u610f\uff1a<\/strong>\u5982\u679c<em><span style=\"color: #008000;\">defaults<\/span><\/em>\u57df\u5305\u542b\u4e86\u201cDEFAULT_SOUND\u201d\uff0c\u90a3\u4e48\u9ed8\u8ba4\u58f0\u97f3\u5c06\u4f1a\u8986\u76d6\u5176\u4ed6\u6240\u6709\u5b9a\u4e49\u8fc7\u7684<em><span style=\"color: #008000;\">sound<\/span><\/em>\u57df\u3002<\/p>\n<h2>\u6dfb\u52a0\u632f\u52a8<\/h2>\n<p>\u53ef\u4ee5\u4f7f\u7528\u9ed8\u8ba4\u632f\u52a8\u6a21\u5f0f\u6216\u662f\u7a0b\u5e8f\u81ea\u5b9a\u4e49\u7684\u632f\u52a8\u6a21\u5f0f\u6765\u63d0\u9192\u7528\u6237\u3002<\/p>\n<p>\u8981\u4f7f\u7528\u9ed8\u8ba4\u6a21\u5f0f\uff0c\u5728<em><span style=\"color: #008000;\">defaults<\/span><\/em>\u57df\u4e2d\u6dfb\u52a0\u201cDEFAULT_VIBRATE\u201d\uff1a<\/p>\n<pre><span style=\"color: #000000;\">notification.defaults |= <span style=\"color: #800080;\">Notification<\/span>.DEFAULT_VIBRATE;<\/span><\/pre>\n<p>\u8981\u5b9a\u4e49\u5c5e\u4e8e\u81ea\u5df1\u7684\u632f\u52a8\u6a21\u5f0f\uff0c\u9700\u5411<em><span style=\"color: #008000;\">vibrate<\/span><\/em>\u57df\u4f20\u9012\u4e00\u7ec4<em>long<\/em>\u578b\u6570\u7ec4\u503c\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #000080;\">long<\/span>[] vibrate = {0,100,200,300};<\/span>\r\n<span style=\"color: #000000;\">notification.vibrate = vibrate;<\/span><\/pre>\n<p>long\u578b\u6570\u7ec4\u5b9a\u4e49\u4e86\u9707\u52a8\u6709\u65e0\u7684\u65f6\u957f\u95f4\u9694\uff08\u4ee5\u6beb\u79d2\u8ba1\uff09\u3002\u7b2c\u4e00\u4e2a\u503c\u662f\u5728\u5f00\u59cb\u4e4b\u524d\u7b49\u5f85\u7684\u65f6\u95f4\uff08\u65e0\u9707\u52a8\uff09\uff0c\u7b2c\u4e8c\u4e2a\u503c\u662f\u9996\u6b21\u9707\u52a8\u7684\u957f\u5ea6\uff0c\u7b2c\u4e09\u4e2a\u503c\u662f\u4e4b\u540e\u7684\u6682\u505c\u65f6\u957f\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002\u632f\u52a8\u6a21\u5f0f\u53ef\u4ee5\u4e3a\u4efb\u610f\u957f\u5ea6\uff0c\u4e0d\u8fc7\u4e0d\u80fd\u8bbe\u7f6e\u5faa\u73af\u3002<\/p>\n<p style=\"padding-left: 30px;\"><strong>\u6ce8\u610f<\/strong>\uff1a\u5982\u679c<em><span style=\"color: #008000;\">defaults<\/span><\/em>\u57df\u5305\u542b\u4e86\u201cDEFAULT_VIBRATE\u201d\uff0c\u90a3\u4e48\u9ed8\u8ba4\u632f\u52a8\u5c06\u8986\u76d6\u6240\u6709\u5728<em><span style=\"color: #008000;\">vibrate<\/span><\/em>\u57df\u4e2d\u5b9a\u4e49\u7684\u9707\u52a8\u3002<\/p>\n<h2>\u6dfb\u52a0\u706f\u5149\u95ea\u70c1<\/h2>\n<p>\u8981\u4f7f\u7528LED\u706f\u5149\u95ea\u70c1\u6765\u63d0\u9192\u7528\u6237\uff0c\u53ef\u4ee5\u7528\u9ed8\u8ba4\u706f\u5149\u6a21\u5f0f\uff08\u5982\u679c\u53ef\u7528\uff09\uff0c\u6216\u81ea\u5b9a\u4e49\u706f\u5149\u6a21\u5f0f\u53ca\u8272\u5f69\u3002<\/p>\n<p>\u8981\u4f7f\u7528\u9ed8\u8ba4\u706f\u5149\u8bbe\u7f6e\uff0c\u5728<em><span style=\"color: #008000;\">defaults<\/span><\/em>\u57df\u4e2d\u6dfb\u52a0\u201cDEFAULT_LIGHTS\u201d\uff1a<\/p>\n<pre><span style=\"color: #000000;\">notification.defaults |=<span style=\"color: #800080;\"> Notification<\/span>.DEFAULT_LIGHTS;<\/span><\/pre>\n<p>\u8981\u81ea\u5b9a\u4e49\u6a21\u5f0f\u548c\u8272\u5f69\uff0c\u9700\u8981\u4e3a<em><span style=\"color: #008000;\">ledARGB<\/span><\/em>\u57df\uff08\u8272\u5f69\uff09\uff0c<em><span style=\"color: #008000;\">ledOffMS<\/span><\/em>\u57df\uff08LED\u5173\u95ed\u65f6\u957f\uff0c\u5355\u4f4d\u6beb\u79d2\uff09\u548c<em><span style=\"color: #008000;\">ledOnMS<\/span><\/em>\u57df\uff08LED\u5f00\u542f\u65f6\u957f\uff0c\u5355\u4f4d\u6beb\u79d2\uff09\u8bbe\u5b9a\u503c\uff0c\u5e76\u5728<em><span style=\"color: #008000;\">flags<\/span><\/em>\u57df\u6dfb\u52a0\u201cFLAG_SHOW_LIGHTS\u201d\uff1a<\/p>\n<pre><span style=\"color: #000000;\">notification.ledARGB = <span style=\"color: #008080;\">0xff00ff00<\/span>;<\/span>\r\n<span style=\"color: #000000;\">notification.ledOnMS = <span style=\"color: #008080;\">300<\/span>;<\/span>\r\n<span style=\"color: #000000;\">notification.ledOffMS = <span style=\"color: #008080;\">1000<\/span>;<\/span>\r\n<span style=\"color: #000000;\">notification.flags |=<span style=\"color: #800080;\"> Notification<\/span>.FLAG_SHOW_LIGHTS;<\/span><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7eff\u706f\u5c06\u53cd\u590d\u95ea\u70c1\uff0c\u4eae300\u6beb\u79d2\uff0c\u66971\u79d2\u3002\u8bbe\u5907\u7684LED\u5e76\u4e0d\u652f\u6301\u5149\u8c31\u4e2d\u6240\u6709\u7684\u8272\u5f69\uff0c\u6bcf\u4e00\u79cd\u8bbe\u5907\u652f\u6301\u7684\u8272\u5f69\u4e5f\u6709\u4e0d\u540c\uff0c\u786c\u4ef6\u4f1a\u5c3d\u53ef\u80fd\u63d0\u4f9b\u6700\u4f73\u6548\u679c\u3002\u7eff\u8272\u662f\u6700\u5e38\u7528\u7684\u901a\u77e5\u8272\u5f69\u3002<\/p>\n<h2>\u66f4\u591a\u7279\u6027<\/h2>\n<p>\u53ef\u4ee5\u901a\u8fc7Notification\u57df\u548c\u65d7\u6807\u5411\u901a\u77e5\u6dfb\u52a0\u66f4\u591a\u7279\u6027\u3002\u5305\u62ec\u4ee5\u4e0b\u6709\u7528\u7684\u7279\u6027\uff1a<\/p>\n<p>\u201cFLAG_AUTO_CANCEL\u201d<\/p>\n<p style=\"padding-left: 30px;\">\u65d7\u6807\u5c06\u5176\u52a0\u5165<em><span style=\"color: #008000;\">flags<\/span><\/em>\u57df\u4ee5\u5728\u901a\u77e5\u88ab\u4ece\u901a\u77e5\u7a97\u53e3\u4e2d\u9009\u62e9\u65f6\u81ea\u52a8\u53d6\u6d88<\/p>\n<p>\u201cFLAG_INSISTENT\u201d<\/p>\n<p style=\"padding-left: 30px;\">\u65d7\u6807\u5c06\u5176\u52a0\u5165<em><span style=\"color: #008000;\">flags<\/span><\/em>\u57df\u4ee5\u91cd\u590d\u97f3\u9891\u64ad\u653e\u76f4\u81f3\u7528\u6237\u56de\u5e94<\/p>\n<p>\u201cFLAG_ONGOING_EVENT\u201d<\/p>\n<p style=\"padding-left: 30px;\">\u65d7\u6807\u5c06\u5176\u52a0\u5165<em><span style=\"color: #008000;\">flags<\/span><\/em>\u57df\u4ee5\u5c06\u901a\u77e5\u5206\u7ec4\u4e8e\u901a\u77e5\u7a97\u53e3\u7684\u201c\u6b63\u5728\u8fdb\u884c\u201d\u6807\u9898\u4e4b\u4e0b\u3002\u8fd9\u8868\u793a\u7a0b\u5e8f\u6b63\u5728\u8fd0\u884c\u2014\u2014\u5b83\u7684\u8fdb\u7a0b\u4ecd\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u5c3d\u7ba1\u7a0b\u5e8f\u672c\u8eab\u4e0d\u53ef\u89c1\uff08\u4f8b\u5982\u97f3\u4e50\u548c\u901a\u8bdd\uff09<\/p>\n<p>\u201cFLAG_NO_CLEAR\u201d<\/p>\n<p style=\"padding-left: 30px;\">\u65d7\u6807\u5c06\u5176\u52a0\u5165<em><span style=\"color: #008000;\">flags<\/span><\/em>\u57df\u4ee5\u8868\u660e\u901a\u77e5\u4e0d\u5e94\u5f53\u88ab\u201c\u6e05\u9664\u901a\u77e5\u201d\u6309\u94ae\u6e05\u9664\u3002\u8fd9\u5728\u7a0b\u5e8f\u6b63\u5728\u8fd0\u884c\u65f6\u5f88\u6709\u7528\u3002<\/p>\n<p><em><span style=\"color: #008000;\">number<\/span><\/em>\u57df<\/p>\n<p style=\"padding-left: 30px;\">\u8fd9\u4e2a\u503c\u663e\u793a\u4e86\u5f53\u524d\u88ab\u901a\u77e5\u6240\u8868\u793a\u7684\u4e8b\u4ef6\u6570\u3002\u6b63\u786e\u7684\u6570\u5b57\u4f1a\u5728\u72b6\u6001\u680f\u56fe\u6807\u9876\u90e8\u663e\u793a\u3002\u5982\u679c\u8981\u7528\u8fd9\u4e2a\u57df\uff0c\u90a3\u5c31\u5fc5\u987b\u5728\u901a\u77e5\u88ab\u521b\u5efa\u65f6\u4ece\u201c1\u201d\u5f00\u59cb\u8ba1\u6570\u3002\uff08\u5982\u679c\u5728\u4e00\u6b21\u66f4\u65b0\u65f6\u5c06\u8fd9\u4e2a\u503c\u7531\u96f6\u6539\u4e3a\u4e86\u6bd4\u96f6\u5927\u7684\u4efb\u4f55\u6570\uff0c\u8fd9\u4e2a\u6570\u5b57\u5c31\u4e0d\u4f1a\u88ab\u663e\u793a\u3002\uff09<\/p>\n<p><em><span style=\"color: #008000;\">iconLevel<\/span><\/em>\u57df<\/p>\n<p style=\"padding-left: 30px;\">\u8fd9\u4e2a\u503c\u663e\u793a\u4e86\u5f53\u524d\u7528\u4e8e\u901a\u77e5\u56fe\u6807\u7684LevelListDrawable\u7b49\u7ea7\u3002\u914d\u5408\u5728LevelListDrawable\u4e2d\u5b9a\u4e49\u7684drawable\uff0c\u53ef\u4ee5\u901a\u8fc7\u6539\u53d8\u8fd9\u4e2a\u503c\u4f7f\u72b6\u6001\u680f\u4e2d\u7684\u56fe\u6807\u5448\u73b0\u52a8\u753b\u6548\u679c\u3002\u53c2\u89c1LevelListDrawable\u4ee5\u83b7\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<p>\u53c2\u89c1Notification\u7c7b\u53c2\u8003\u8d44\u6599\u4ee5\u83b7\u53d6\u5173\u4e8e\u6dfb\u52a0\u7279\u6027\u6765\u81ea\u5b9a\u4e49\u7a0b\u5e8f\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<h2>\u521b\u5efa\u81ea\u5b9a\u4e49\u6269\u5c55\u89c6\u56fe<\/h2>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u7528\u4e8e\u901a\u77e5\u7a97\u53e3\u7684\u6269\u5c55\u89c6\u56fe\u5305\u62ec\u57fa\u672c\u7684\u6807\u9898\u548c\u6587\u672c\u6d88\u606f\u3002\u8fd9\u4e9b\u662f\u7531setLatestEventInfo()\u65b9\u6cd5\u7684<em><span style=\"color: #008000;\">contentTitle<\/span><\/em>\u548c<em><span style=\"color: #008000;\">contentText<\/span><\/em>\u53c2\u6570\u6240\u5b9a\u4e49\u3002\u7136\u800c\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528RemotiViews\u6765\u81ea\u5b9a\u4e49\u6269\u5c55\u89c6\u56fe\u7684\u5e03\u5c40\u3002\u4e0b\u9762\u7684\u5c4f\u5e55\u622a\u56fe\u5c55\u793a\u4e86\u4e00\u4e2a\u4ee5\u7ebf\u6027\u5e03\u5c40\u4f7f\u7528ImageView\u548cTextView\u7684\u81ea\u5b9a\u4e49\u6269\u5c55\u89c6\u56fe\u3002<\/p>\n<p><a href=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/04\/custom_message.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-486\" title=\"custom_message\" src=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/04\/custom_message.png\" alt=\"\" width=\"200\" height=\"157\" \/><\/a>\u8981\u5b9a\u4e49\u6269\u5c55\u6d88\u606f\u81ea\u6709\u7684\u5e03\u5c40\uff0c\u9700\u8981\u5b9e\u4f8b\u5316RemoteViews\u5bf9\u8c61\u5e76\u5c06\u5176\u4f20\u9012\u7ed9Notification\u7684<em><span style=\"color: #008000;\">contentView<\/span><\/em>\u57df\u3002\u8fd8\u8981\u5c06PendingIntent\u4f20\u9012\u7ed9<em><span style=\"color: #008000;\">contentIntent<\/span><\/em>\u57df\u3002<\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u8303\u4f8b\u6765\u66f4\u597d\u7684\u7406\u89e3\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6269\u5c55\u89c6\u56fe\uff1a<\/p>\n<p>1.\u521b\u5efa\u6269\u5c55\u89c6\u56fe\u7684XML\u5e03\u5c40\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<span style=\"color: #008000;\">custom_notification_leyout.xml<\/span>\u7684\u5e03\u5c40\u6587\u4ef6\u5e76\u50cf\u8fd9\u6837\u7f16\u5199\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #000080;\">&lt;LinearLayout<\/span> <span style=\"color: #800080;\">xmlns:android<\/span>=<span style=\"color: #008000;\">\"http:\/\/schemas.android.com\/apk\/res\/android\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:orientation<\/span>=<span style=\"color: #008000;\">\"horizontal\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_width<\/span>=<span style=\"color: #008000;\">\"fill_parent\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_height<\/span>=<span style=\"color: #008000;\">\"fill_parent\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:padding<\/span>=<span style=\"color: #008000;\">\"3dp\"<\/span><\/span>\r\n<span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &gt;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 &lt;ImageView<\/span> <span style=\"color: #800080;\">android:id<\/span>=<span style=\"color: #008000;\">\"@+id\/image\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_width<\/span>=<span style=\"color: #008000;\">\"wrap_content\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_height<\/span>=<span style=\"color: #008000;\">\"fill_parent\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_marginRight<\/span>=<span style=\"color: #008000;\">\"10dp\"<\/span><\/span>\r\n<span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/&gt;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 &lt;TextView<\/span> <span style=\"color: #800080;\">android:id<\/span>=<span style=\"color: #008000;\">\"@+id\/text\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_width<\/span>=<span style=\"color: #008000;\">\"wrap_content\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:layout_height<\/span>=<span style=\"color: #008000;\">\"fill_parent\"<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 android:textColor<\/span>=<span style=\"color: #008000;\">\"#000\"<\/span><\/span>\r\n<span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/&gt;<\/span>\r\n<span style=\"color: #000080;\">&lt;\/LinearLayout&gt;<\/span><\/pre>\n<p>\u8be5\u5e03\u5c40\u5c06\u88ab\u7528\u4e8e\u6269\u5c55\u89c6\u56fe\uff0c\u4e0d\u8fc7ImageView\u548cTextView\u7684\u5185\u5bb9\u4ecd\u9700\u8981\u7531\u7a0b\u5e8f\u6765\u5b9a\u4e49\u3002RemoteViews\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7528\u4ee5\u5b9a\u4e49\u8fd9\u4e9b\u5185\u5bb9\u7684\u4fbf\u5229\u65b9\u6cd5\u2026<\/p>\n<p>2.\u5728\u7a0b\u5e8f\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u4e86RemoveViews\u65b9\u6cd5\u6765\u5b9a\u4e49\u4e86\u56fe\u7247\u548c\u6587\u672c\u3002\u4e4b\u540e\u5c06RemoteViews\u5bf9\u8c61\u4f20\u9012\u81f3Notification\u7684contentView\u57df\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #800080;\">RemoteViews<\/span> contentView = <span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">RemoteViews<\/span>(getPackageName(), R.layout.custom_notification_layout);<\/span>\r\n<span style=\"color: #000000;\">contentView.setImageViewResource(R.id.image, R.drawable.notification_image);<\/span>\r\n<span style=\"color: #000000;\">contentView.setTextViewText(R.id.text, <span style=\"color: #008000;\">\"Hello, this message is in a custom expanded view\"<\/span>);<\/span>\r\n<span style=\"color: #000000;\">notification.contentView = contentView;<\/span><\/pre>\n<p>\u5982\u4e0a\u6240\u793a\uff0c\u5c06\u7a0b\u5e8f\u7684package\u540d\u548c\u5e03\u5c40\u8d44\u6e90ID\u4f20\u9012\u7ed9RemoteViews\u7684\u6784\u9020\u51fd\u6570\u3002\u4e4b\u540e\uff0c\u901a\u8fc7setImageViewResourse()\u548csetTextViewText()\u6765\u5b9a\u4e49ImageView\u548cTextView\u7684\u5185\u5bb9\u3002\u4e24\u8005\u90fd\u9700\u8981\u4f20\u9012\u5e0c\u671b\u4f7f\u7528\u7684View\u5bf9\u8c61\u7684\u6b63\u786e\u7684\u5f15\u7528ID\u4ee5\u53ca\u5176\u503c\u3002\u6700\u540eRemoteViews\u5bf9\u8c61\u88ab\u4f20\u9012\u5230\u4e86Notification\u7684<em><span style=\"color: #008000;\">contentView<\/span><\/em>\u57df\u3002<\/p>\n<p>3.\u56e0\u4e3a\u5728\u4f7f\u7528\u81ea\u5b9a\u4e49\u89c6\u56fe\u65f6\u4e0d\u9700\u8981\u4f7f\u7528setLatestEventInfo()\u65b9\u6cd5\uff0c\u6240\u4ee5\u5fc5\u987b\u5728<em><span style=\"color: #008000;\">contentIntent<\/span><\/em>\u57df\u4e2d\u5b9a\u4e49Notification\u6240\u4f7f\u7528\u7684Intent\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #800080;\">Intent<\/span> notificationIntent = <span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">Intent<\/span>(<span style=\"color: #000080;\">this<\/span>, <span style=\"color: #800080;\">MyClass<\/span>.<span style=\"color: #000080;\">class<\/span>);<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">PendingIntent<\/span> contentIntent = <span style=\"color: #800080;\">PendingIntent<\/span>.getActivity(<span style=\"color: #000080;\">this<\/span>, 0, notificationIntent, 0);<\/span>\r\n<span style=\"color: #000000;\">notification.contentIntent = contentIntent;<\/span><\/pre>\n<p>4.\u73b0\u5728\u901a\u77e5\u5c31\u53ef\u4ee5\u50cf\u5f80\u5e38\u4e00\u6837\u88ab\u53d1\u9001\u4e86\uff1a<\/p>\n<pre><span style=\"color: #000000;\">mNotificationManager.notify(CUSTOM_VIEW_ID, notification);<\/span><\/pre>\n<p>RemoteViews\u7c7b\u8fd8\u5305\u62ec\u53ef\u4ee5\u7528\u6765\u5728\u901a\u77e5\u7684\u6269\u5c55\u89c6\u56fe\u4e2d\u8f7b\u677e\u6dfb\u52a0Chronometer\uff08\u8ba1\u65f6\u5668\uff09\u6216\u662fProgressBar\uff08\u8fdb\u5ea6\u6761\uff09\u7684\u65b9\u6cd5\u3002\u5173\u4e8e\u901a\u8fc7RemoteViews\u6765\u521b\u5efa\u81ea\u5b9a\u4e49\u5e03\u5c40\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u8003RemoteViews\u7c7b\u7684\u53c2\u8003\u8d44\u6599\u3002<\/p>\n<p style=\"padding-left: 30px;\"><strong>\u6ce8\u610f<\/strong>\uff1a\u5728\u521b\u5efa\u81ea\u5b9a\u4e49\u6269\u5c55\u89c6\u56fe\u65f6\uff0c\u5fc5\u987b\u975e\u5e38\u5c0f\u5fc3\u4ee5\u786e\u4fdd\u81ea\u5b9a\u4e49\u5e03\u5c40\u5728\u4e0d\u540c\u8bbe\u5907\u65b9\u5411\uff08orientation\uff0c\u5373\u6a2a\u5c4f\u6216\u662f\u7ad6\u5c4f\uff09\u548c\u5206\u8fa8\u7387\u4e0b\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002\u5c3d\u7ba1\u8fd9\u4e2a\u5efa\u8bae\u5bf9\u4e8eAndroid\u4e0a\u521b\u5efa\u7684\u6240\u6709View\u5e03\u5c40\u6765\u8bf4\u90fd\u662f\u503c\u5f97\u6ce8\u610f\u7684\uff0c\u4f46\u5728\u8fd9\u4e00\u90e8\u5206\u91cc\u5c24\u4e3a\u91cd\u8981\uff0c\u56e0\u4e3a\u5e03\u5c40\u6240\u53d7\u7684\u9650\u5236\u975e\u5e38\u4e25\u683c\u3002\u6240\u4ee5\u4e0d\u8981\u5c06\u81ea\u5b9a\u4e49\u5e03\u5c40\u8bbe\u8ba1\u7684\u8fc7\u4e8e\u590d\u6742\uff0c\u6b64\u5916\u5e94\u5f53\u5728\u591a\u79cd\u4e0d\u540c\u60c5\u51b5\u4e0b\u4f5c\u6d4b\u8bd5\u3002<\/p>\n<p>\u8fd4\u56de<a title=\"\u901a\u77e5\u7528\u6237\" href=\"http:\/\/leybreeze.com\/blog\/?p=452\">\u901a\u77e5\u7528\u6237<\/a><\/p>\n<p>\u672c\u4f5c\u54c1\u91c7\u7528<a title=\"\u77e5\u8bc6\u5171\u4eab \u7f72\u540d-\u975e\u5546\u4e1a\u6027\u4f7f\u7528-\u7981\u6b62\u6f14\u7ece 3.0 Unported\u8bb8\u53ef\u534f\u8bae\" rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-nc-nd\/3.0\/\" target=\"_blank\">\u77e5\u8bc6\u5171\u4eab \u7f72\u540d-\u975e\u5546\u4e1a\u6027\u4f7f\u7528-\u7981\u6b62\u6f14\u7ece 3.0 Unported\u8bb8\u53ef\u534f\u8bae<\/a>\u8fdb\u884c\u8bb8\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u72b6\u6001\u680f\u901a\u77e5\u4f1a\u5411\u7cfb\u7edf\u72b6\u6001\u680f\u6dfb\u52a0\u4e00\u4e2a\u56fe\u6807\uff08\u8fd8\u53ef\u4ee5\u9644\u6709\u6587\u672c\u6d88\u606f\uff09\u4ee5\u53ca\u4e00\u6761\u201c\u901a\u77e5\u201d\u7a97\u53e3\u5185\u7684\u53ef\u6269\u5c55\u6d88\u606f\u3002\u5f53\u7528\u6237\u9009\u62e9\u4e86\u8be5\u53ef &hellip; <a href=\"http:\/\/leybreeze.com\/blog\/?p=470\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u521b\u5efa\u72b6\u6001\u680f\u901a\u77e5\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,10],"tags":[5,57],"_links":{"self":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/470"}],"collection":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=470"}],"version-history":[{"count":15,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/470\/revisions"}],"predecessor-version":[{"id":489,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/470\/revisions\/489"}],"wp:attachment":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}