{"id":618,"date":"2013-07-16T14:28:36","date_gmt":"2013-07-16T06:28:36","guid":{"rendered":"http:\/\/www.yeetrack.com\/?p=618"},"modified":"2013-09-27T22:36:35","modified_gmt":"2013-09-27T14:36:35","slug":"android%e8%ae%be%e7%bd%ae%e9%80%9a%e7%9f%a5notification","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=618","title":{"rendered":"Android\u8bbe\u7f6e\u901a\u77e5Notification"},"content":{"rendered":"<p>Android\u7cfb\u7edf\u63d0\u4f9bNotification\u548cNotificationManagement\u4e24\u4e2a\u7c7b\u6765\u7ba1\u7406\u5b9e\u73b0\u901a\u77e5\uff0c\u901a\u8fc7\u8fd9\u4e24\u4e2a\u7c7b\u6211\u4eec\u53ef\u4ee5\u663e\u793a\u5e7f\u64ad\u4fe1\u606f\u7684\u5185\u5bb9\uff0c\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u901a\u77e5\u7684\u94c3\u58f0\u3001\u9707\u52a8\u3001\u95ea\u5149\u706f\u3002\u901a\u8fc7Activity<code>\u7c7b\u4e2d\u7684getSystemService<\/code>\u8fd9\u4e2a\u65b9\u6cd5\u83b7\u53d6NotificationManagement\uff0c\u518d\u5b9e\u4f8b\u5316Notification\uff0c\u6700\u540e\u518d\u7528NotificationManagement\u53d1\u9001\u51fa\u53bb\u5373\u53ef\u3002\u6ce8\uff1a\u83b7\u53d6Notification\u5bf9\u8c61\u65f6\uff0candroid api 11\u4e4b\u540e\u5df2\u7ecf\u4e0d\u63a8\u8350\u4f7f\u7528<code>notification.setLatestEventInfo()<\/code>\u65b9\u6cd5\u4e86\uff0c\u63a8\u8350\u4f7f\u7528<code>Notification.Builder<\/code> \u6765\u5b9e\u4f8b\u5316Notification\u3002\u6837\u4f8b\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre><code>package com.yeetrack.broadcast;\n\nimport android.net.Uri;<!--more-->\nimport android.os.Bundle;\nimport android.os.Environment;\nimport android.view.View;\nimport android.view.View.OnClickListener;\nimport android.widget.Button;\nimport android.app.Activity;\nimport android.app.Notification;\nimport android.app.NotificationManager;\nimport android.app.PendingIntent;\nimport android.content.Intent;\nimport android.graphics.Color;\n\npublic class MainActivity extends Activity \n{\n\n    private Button button1;\n    private Button button2; \n\n    private Notification notification;\n    private NotificationManager notificationManager;\n    private static final int ID = 1;\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) \n    {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n        button1 = (Button)findViewById(R.id.button1Id);\n        button2 = (Button)findViewById(R.id.button2Id);\n        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);\n        notification = new Notification();\n        notification.icon = R.drawable.ic_launcher;\n        notification.tickerText = \"\u6211\u7684\u901a\u77e5\";\n        notification.when = System.currentTimeMillis();\n\n        OnClickListener onClickListener = new OnClickListener()\n        {\n\n            @Override\n            public void onClick(View v)\n            {\n                switch( v.getId())\n                {\n                case R.id.button1Id:\n                    Intent intent = new Intent(MainActivity.this, MainActivity.class);\n\n                    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);\n                    long[] vibrate = {0,100,200,300};\n                    Notification.Builder builder = new Notification.Builder(MainActivity.this)\n                        .setContentIntent(pendingIntent)\n                        .setSmallIcon(R.drawable.ic_launcher)\n                        .setTicker(\"\u6211\u7684\u6d88\u606f\")\n                        .setWhen(System.currentTimeMillis())\n                        .setContentTitle(\"\u6807\u9898\")\n                        .setContentText(\"\u5185\u5bb9\")\n                        .setSound(Uri.parse(Environment.getExternalStorageDirectory().getPath()+\"\/sound.mp3\"))\n                        .setVibrate(vibrate)\n                        .setLights(Color.RED, 500, 500);\n                    notification = builder.build();\n                    notificationManager.notify(ID, notification);\n                    break;\n                case R.id.button2Id:\n                    notificationManager.cancel(ID);\n                    break;\n                }\n\n        }\n    };\n    button1.setOnClickListener(onClickListener);\n    button2.setOnClickListener(onClickListener);\n}\n\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Android\u7cfb\u7edf\u63d0\u4f9bNotification\u548cNotificationManagement\u4e24\u4e2a\u7c7b\u6765\u7ba1\u7406\u5b9e\u73b0\u901a\u77e5\uff0c\u901a\u8fc7\u8fd9\u4e24\u4e2a\u7c7b\u6211\u4eec\u53ef\u4ee5\u663e\u793a\u5e7f\u64ad\u4fe1\u606f\u7684\u5185\u5bb9\uff0c\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u901a\u77e5\u7684\u94c3\u58f0\u3001\u9707\u52a8\u3001\u95ea\u5149\u706f\u3002\u901a\u8fc7Act&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[33],"tags":[43,8,7,16],"class_list":["post-618","post","type-post","status-publish","format-standard","hentry","category-coding","tag-android","tag-java","tag-7","tag-16"],"views":3418,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/618","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=618"}],"version-history":[{"count":1,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/618\/revisions"}],"predecessor-version":[{"id":619,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/618\/revisions\/619"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}