{"id":1290,"date":"2022-10-10T22:49:44","date_gmt":"2022-10-10T14:49:44","guid":{"rendered":"https:\/\/fireinsect.top\/?p=1290"},"modified":"2024-03-21T17:03:28","modified_gmt":"2024-03-21T09:03:28","slug":"kotlin%e5%ae%89%e5%8d%93%e5%bc%80%e5%8f%91%e5%ad%a6%e4%b9%a0-intent%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fireinsect.top\/index.php\/2022\/10\/10\/kotlin%e5%ae%89%e5%8d%93%e5%bc%80%e5%8f%91%e5%ad%a6%e4%b9%a0-intent%e7%9a%84%e4%bd%bf%e7%94%a8\/","title":{"rendered":"Kotlin\u5b89\u5353\u5f00\u53d1\u5b66\u4e60&#8211;Intent\u7684\u4f7f\u7528"},"content":{"rendered":"\n<p>Intent\u662fAndroid\u7a0b\u5e8f\u4e2d\u5404\u7ec4\u4ef6\u4e4b\u95f4\u8fdb\u884c\u4ea4\u4e92\u7684\u4e00\u79cd\u91cd\u8981\u65b9\u5f0f\uff0c\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u6307\u660e\u5f53\u524d\u7ec4\u4ef6\u60f3\u8981\u6267\u884c\u7684\u52a8\u4f5c\uff0c\u8fd8\u53ef\u4ee5\u5728\u4e0d\u540c\u7ec4\u4ef6\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Intent\u5927\u81f4\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\uff1a\u663e\u5f0fIntent\u548c\u9690\u5f0fIntent\u3002<\/p>\n\n\n\n<p>Intent\u5b58\u5728\u591a\u4e2a\u6784\u9020\u51fd\u6570\u7684\u91cd\u8f7d\uff0c\u5176\u4e2d\u6700\u57fa\u672c\u7684\u4e00\u4e2aIntent(Context packageContext, Class&lt;?> cls)\u524d\u4e00\u4e2a\u53c2\u6570\u63a5\u53d7\u4e00\u4e2a\u542f\u52a8activity\u7684\u4e0a\u4e0b\u6587\uff0c\u540e\u4e00\u4e2a\u53c2\u6570\u63d0\u4f9b\u9700\u8981\u542f\u52a8\u7684Activity\u3002\u4e4b\u540e\u6211\u4eec\u5c31\u80fd\u4f7f\u7528startActivity\u65b9\u6cd5\u6765\u542f\u52a8Activity\uff0c\u800c\u8fd9\u4e2a\u65b9\u6cd5\u6b63\u662f\u63a5\u53d7\u4e00\u4e2aIntent\u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val intent = Intent(this, SecondActivity::class.java)\nstartActivity(intent)<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u662f\u663e\u5f0fIntent\u7684\u7528\u6cd5\uff0c\u5b83\u901a\u8fc7\u6307\u5b9a\u4e00\u4e2a\u660e\u786e\u7684Activity\u6765\u5b9e\u73b0&#8221;\u610f\u56fe&#8221;\u6307\u5b9a\u3002<\/p>\n\n\n\n<p>\u800c\u9690\u5f0fIntent\u5219\u4e0d\u540c\uff0c\u5b83\u901a\u8fc7\u6307\u5b9a\u66f4\u4e3a\u62bd\u8c61\u7684action\u548ccategory\u7b49\u4fe1\u606f\uff0c\u7136\u540e\u4ea4\u7531\u7cfb\u7edf\u53bb\u5206\u6790\u8fd9\u4e2aIntent\uff0c\u5e76\u5bfb\u627e\u5408\u9002\u7684Activity\u6765\u542f\u52a8\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\u5982\u4f55\u53bb\u8bbe\u5b9a\u4e00\u4e2aActivity\u7684action\u548ccategory\u7b49\u4fe1\u606f\uff1f\u5f53\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Activity\u65f6\uff0cAndroid Studio\u4f1a\u81ea\u52a8\u5728Mainifest\u5185\u6ce8\u518c\u8be5Activity\u4fe1\u606f\u3002\u800c\u6211\u4eec\u5219\u53ef\u4ee5\u5728\u5176\u4e2d\u914d\u7f6e\u4fe1\u606f\u5185\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"xml\" class=\"language-xml\">&lt;activity android:name=\".SecondActivity\" >\n     &lt;intent-filter>\n         &lt;action android:name=\"com.example.activitytest.ACTION_START\" \/>\n         &lt;category android:name=\"android.intent.category.DEFAULT\" \/>\n         &lt;category android:name=\"com.example.activitytest.MY_CATEGORY\"\/>\n     &lt;\/intent-filter>\n&lt;\/activity><\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0c\u53ef\u4ee5\u5728&lt;activity>\u6807\u7b7e\u5185\u4f7f\u7528&lt;intent-filter>\u6765\u914d\u7f6e\u610f\u56fe\u8fc7\u6ee4\u5668\uff0c\u5728\u5176\u4e2d\u5219\u53ef\u4ee5\u914d\u7f6eaction\u548ccategory\u7b49\u4fe1\u606f\u3002<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\">\u6ce8\u610f\u7684\u662f\uff0c\u53ea\u6709\u5f53action\u548ccategory\u5185\u6240\u6709\u5185\u5bb9\u4e0eintent\u5339\u914d\u65f6\u8fd9\u4e2aIntent\u624d\u80fd\u54cd\u5e94\u6b64Activity<\/p>\n\n\n\n<p class=\"has-vivid-green-cyan-color has-text-color\">\u6bcf\u4e2aIntent\u4e2d\u53ea\u80fd\u6307\u5b9a\u4e00\u4e2aaction\uff0c\u4f46\u80fd\u6307\u5b9a\u591a\u4e2acategory<\/p>\n\n\n\n<p>\u4f46\u5982\u679ccategory\u53ea\u914d\u7f6e\u4e86DEFAULT\u5c5e\u6027\u5219\u4e0d\u9700\u8981\u518d\u5728intent\u4e2d\u914d\u7f6ecategory\uff0c\u56e0\u4e3a\u8fd9\u5df2\u7ecf\u662f\u9ed8\u8ba4\u4e86\u3002<\/p>\n\n\n\n<p>\u4e4b\u540e\u6211\u4eec\u9700\u8981\u542f\u52a8\u5219\u53ef\u4ee5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">button1.setOnClickListener {\n     val intent = Intent(\"com.example.activitytest.ACTION_START\")\n     startActivity(intent)\n}<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u9690\u5f0fIntent\u5b58\u5728\u522b\u7684\u7528\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528intent.data\u6765\u4f20\u5165\u503c\u3002\u6bd4\u5982\u4f7f\u7528<code>intent.data = Uri.parse(\"https:\/\/www.baidu.com\")<\/code>\u6765\u4f20\u5165\u5c06\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3aURI\u5bf9\u8c61\uff0c\u5e76\u4f20\u5165intent\u5185\u3002\u4f46\u8fd9\u6709\u4ec0\u4e48\u7528\u5462\uff1f<\/p>\n\n\n\n<p>\u6bd4\u5982\u524d\u9762\u6211\u4eec\u4f20\u5165\u7684URI,\u5982\u679cintent\u7684action\u662f\u6253\u5f00\u7f51\u9875\uff0c\u5219\u901a\u8fc7\u4f20\u503c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6253\u5f00\u56fa\u5b9a\u7f51\u9875\u7684\u6548\u679c\u3002<\/p>\n\n\n\n<p>\u4e0e\u6b64\u5bf9\u5e94\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u6807\u7b7e\u4e2d\u518d\u914d\u7f6e\u4e00\u4e2a\u6807\u7b7e\uff0c\u7528\u4e8e\u66f4\u7cbe\u786e\u5730\u6307\u5b9a\u5f53\u524dActivity\u80fd\u591f\u54cd\u5e94\u7684\u6570\u636e\u3002\u6807\u7b7e\u4e2d\u4e3b\u8981\u53ef\u4ee5\u914d\u7f6e\u4ee5\u4e0b\u5185\u5bb9\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>android:scheme\u3002\u7528\u4e8e\u6307\u5b9a\u6570\u636e\u7684\u534f\u8bae\u90e8\u5206\uff0c\u5982\u4e0a\u4f8b\u4e2d\u7684https\u90e8\u5206\u3002<\/li><li>android:host\u3002\u7528\u4e8e\u6307\u5b9a\u6570\u636e\u7684\u4e3b\u673a\u540d\u90e8\u5206\uff0c\u5982\u4e0a\u4f8b\u4e2d\u7684www.baidu.com\u90e8\u5206\u3002<\/li><li>android:port\u3002\u7528\u4e8e\u6307\u5b9a\u6570\u636e\u7684\u7aef\u53e3\u90e8\u5206\uff0c\u4e00\u822c\u7d27\u968f\u5728\u4e3b\u673a\u540d\u4e4b\u540e\u3002<\/li><li>android:path\u3002\u7528\u4e8e\u6307\u5b9a\u4e3b\u673a\u540d\u548c\u7aef\u53e3\u4e4b\u540e\u7684\u90e8\u5206\uff0c\u5982\u4e00\u6bb5\u7f51\u5740\u4e2d\u8ddf\u5728\u57df\u540d\u4e4b\u540e\u7684\u5185\u5bb9\u3002<\/li><li>android:mimeType\u3002\u7528\u4e8e\u6307\u5b9a\u53ef\u4ee5\u5904\u7406\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5141\u8bb8\u4f7f\u7528\u901a\u914d\u7b26\u7684\u65b9\u5f0f\u8fdb\u884c\u6307\u5b9a\u3002<\/li><\/ol>\n\n\n\n<p>\u53ea\u6709\u5f53\u6807\u7b7e\u4e2d\u6307\u5b9a\u7684\u5185\u5bb9\u548cIntent\u4e2d\u643a\u5e26\u7684Data\u5b8c\u5168\u4e00\u81f4\u65f6\uff0c\u5f53\u524dActivity\u624d\u80fd\u591f\u54cd\u5e94\u8be5Intent\u3002\u4e0d\u8fc7\uff0c\u5728\u6807\u7b7e\u4e2d\u4e00\u822c\u4e0d\u4f1a\u6307\u5b9a\u8fc7\u591a\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<p>\u9664\u4e86https\u534f\u8bae\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u6307\u5b9a\u5f88\u591a\u5176\u4ed6\u534f\u8bae\uff0c\u6bd4\u5982geo\u8868\u793a\u663e\u793a\u5730\u7406\u4f4d\u7f6e\u3001tel\u8868\u793a\u62e8\u6253\u7535\u8bdd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">val intent = Intent(Intent.ACTION_DIAL)<br>intent.data = Uri.parse(\"tel:10086\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Intent\u662fAndroid\u7a0b\u5e8f\u4e2d\u5404\u7ec4\u4ef6\u4e4b\u95f4\u8fdb\u884c\u4ea4\u4e92\u7684\u4e00\u79cd\u91cd\u8981\u65b9\u5f0f\uff0c\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u6307\u660e\u5f53\u524d\u7ec4\u4ef6\u60f3\u8981\u6267\u884c\u7684\u52a8\u4f5c\uff0c\u8fd8\u53ef [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[16],"tags":[],"class_list":["post-1290","post","type-post","status-publish","format-standard","hentry","category-16"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1290"}],"collection":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/comments?post=1290"}],"version-history":[{"count":4,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1290\/revisions"}],"predecessor-version":[{"id":1296,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1290\/revisions\/1296"}],"wp:attachment":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/media?parent=1290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/categories?post=1290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/tags?post=1290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}