{"id":94,"date":"2014-08-24T23:09:36","date_gmt":"2014-08-24T15:09:36","guid":{"rendered":"http:\/\/www.yarpee.com\/?p=94"},"modified":"2014-08-24T23:09:36","modified_gmt":"2014-08-24T15:09:36","slug":"interacting-with-other-apps","status":"publish","type":"post","link":"http:\/\/www.yarpee.cn\/?p=94","title":{"rendered":"Interacting with Other Apps"},"content":{"rendered":"<div><span style=\"color: #000000;\">App\u5185\u90e8\u901a\u8fc7\u521b\u5efaIntent\u5e76\u663e\u793a\u7684\u6307\u660e\u9700\u8981start\u7684\u7ec4\u4ef6\u7c7b\u540d\u6765\u5bfc\u822a\u5230\u53e6\u4e00\u4e2aActivity\uff0c\u800c\u5bfc\u822a\u5230\u53e6\u4e00\u4e2a\u72ec\u7acb\u7684app\u5219\u4e0d\u9700\u8981\u8fd9\u79cd\u663e\u793a\u6307\u660e\u3002\u53ea\u9700\u8981\u5bf9Intent\u8bbe\u7f6e\u6b63\u786e\u7684MIME\u7c7b\u578b\u548c\u76f8\u5e94\u7684data\u5373\u53ef\uff0c\u4f46\u5728invoke Intent\u4e4b\u524d\u9700\u8981\u9a8c\u8bc1\u8bbe\u5907\u4e0a\u662f\u5426\u5df2\u5b89\u88c5\u80fd\u5904\u7406\u8fd9\u7c7bIntent\u7684app\uff0c\u5426\u5219\u4f60\u7684app\u5c06\u4f1a\u53d1\u751fcrash\u3002<\/span><\/p>\n<div style=\"color: #000000;\"><\/div>\n<div style=\"color: #000000;\">\u9a8c\u8bc1\u662f\u5426\u6709Activity\u80fd\u5904\u7406\u67d0\u7c7bIntent\u53ef\u4ee5\u901a\u8fc7queryIntentActivities()\u6765\u505a\uff1a<\/div>\n<div style=\"color: #000000;\">\n<pre style=\"color: #006600;\"><span style=\"color: #660066;\">PackageManager<\/span><span style=\"color: #000000;\"> packageManager <\/span><span style=\"color: #666600;\">=<\/span><code><a style=\"color: #258aaf;\" href=\"http:\/\/developer.android.com\/reference\/android\/content\/Context.html#getPackageManager()\"><span style=\"color: #000000;\">getPackageManager<\/span><span style=\"color: #666600;\">()<\/span><\/a><\/code><span style=\"color: #666600;\">;<\/span><span style=\"color: #000000;\">\r\n<\/span><span style=\"color: #660066;\">List<\/span><span style=\"color: #666600;\">&lt;<\/span><span style=\"color: #660066;\">ResolveInfo<\/span><span style=\"color: #666600;\">&gt;<\/span><span style=\"color: #000000;\"> activities <\/span><span style=\"color: #666600;\">=<\/span><span style=\"color: #000000;\"> packageManager<\/span><span style=\"color: #666600;\">.<\/span><span style=\"color: #000000;\">queryIntentActivities<\/span><span style=\"color: #666600;\">(<\/span><span style=\"color: #000000;\">intent<\/span><span style=\"color: #666600;\">,<\/span><span style=\"color: #006666;\">0<\/span><span style=\"color: #666600;\">);<\/span><span style=\"color: #000000;\">\r\n<\/span><span style=\"color: #000088;\">boolean<\/span><span style=\"color: #000000;\"> isIntentSafe <\/span><span style=\"color: #666600;\">=<\/span><span style=\"color: #000000;\"> activities<\/span><span style=\"color: #666600;\">.<\/span><span style=\"color: #000000;\">size<\/span><span style=\"color: #666600;\">()<\/span><span style=\"color: #666600;\">&gt;<\/span><span style=\"color: #006666;\">0<\/span><span style=\"color: #666600;\">;<\/span><\/pre>\n<\/div>\n<div style=\"color: #000000;\">\u4e00\u65e6\u521b\u5efa\u597dIntent\u5e76\u8c03\u7528startActivity()\uff0c\u5982\u679c\u6709\u8d85\u8fc71\u4e2aActivity\u80fd\u5904\u7406\u8fd9\u7c7bIntent\uff0c\u7cfb\u7edf\u5c06\u4f1a\u5f39\u51fa\u5bf9\u8bdd\u6846\u4f9b\u7528\u6237\u9009\u62e9\u54ea\u4e2aapp\u6765\u6253\u5f00\u3002\u7cfb\u7edf\u5f39\u51fa\u7684\u8be5\u5bf9\u8bdd\u6846\u5e95\u90e8\u4f1a\u6709\u4e00\u4e2acheckbox\uff0c\u7528\u6237\u52fe\u9009\u540e\u4ee5\u540e\u5c06\u9ed8\u8ba4\u4f7f\u7528\u8be5app\u5904\u7406\u8fd9\u7c7bIntent\u3002\u4f46\u6709\u65f6\u5019\u4f60\u9700\u8981\u7528\u6237\u6bcf\u6b21\u90fd\u505a\u9009\u62e9\uff08\u4f8b\u5982\u63d0\u4f9b\u4e0d\u540c\u7684\u5206\u4eab\u5e73\u53f0\uff09\uff0c\u8fd9\u65f6\u521b\u5efa\u7684Intent\u5c31\u9700\u8981\u8c03\u7528createChooser()\u6765\u521b\u5efa\u4e00\u4e2a\u9009\u62e9\u5bf9\u8bdd\u6846\u3002<\/div>\n<div style=\"color: #000000;\"><\/div>\n<div style=\"color: #000000;\">\u5f53\u4f60\u9700\u8981\u4ece\u8c03\u7528\u7684Activity\u53d6\u56de\u7ed3\u679c\u65f6\uff08\u65e0\u8bba\u662f\u81ea\u5df1\u7684\u8fd8\u662f\u5176\u5b83app\u7684\uff09\uff0c\u5e94\u8be5\u8c03\u7528startActivityForResult()\u3002\u8be5API\u591a\u4e86\u4e00\u4e2a\u53c2\u6570request code\uff0c\u5f53\u88ab\u8c03\u7528\u7684Activity\u6709\u7ed3\u679c\u8fd4\u56de\u65f6\u4f1a\u8c03\u7528onActivityResult()\u56de\u8c03\uff0c\u540c\u65f6\u5c06request code\u5e26\u56de\u4f9b\u4f60\u5224\u65ad\u5982\u4f55\u5904\u7406\u8fd4\u56de\u7ed3\u679c\u3002<\/div>\n<div style=\"color: #000000;\">\n<pre style=\"color: #006600;\"><span style=\"color: #006666;\">@Override<\/span><span style=\"color: #000000;\">\r\n<\/span><span style=\"color: #000088;\">protected<\/span><span style=\"color: #000088;\">void<\/span><span style=\"color: #000000;\"> onActivityResult<\/span><span style=\"color: #666600;\">(<\/span><span style=\"color: #000088;\">int<\/span><span style=\"color: #000000;\"> requestCode<\/span><span style=\"color: #666600;\">,<\/span><span style=\"color: #000088;\">int<\/span><span style=\"color: #000000;\"> resultCode<\/span><span style=\"color: #666600;\">,<\/span><span style=\"color: #660066;\">Intent<\/span><span style=\"color: #000000;\"> data<\/span><span style=\"color: #666600;\">)<\/span><span style=\"color: #666600;\">{<\/span><span style=\"color: #000000;\">\r\n    <\/span>\/\/ Check which request we're responding to<span style=\"color: #000000;\">\r\n    <\/span><span style=\"color: #000088;\">if<\/span><span style=\"color: #666600;\">(<\/span><span style=\"color: #000000;\">requestCode <\/span><span style=\"color: #666600;\">==<\/span><span style=\"color: #000000;\"> PICK_CONTACT_REQUEST<\/span><span style=\"color: #666600;\">)<\/span><span style=\"color: #666600;\">{<\/span><span style=\"color: #000000;\">\r\n        <\/span>\/\/ Make sure the request was successful<span style=\"color: #000000;\">\r\n        <\/span><span style=\"color: #000088;\">if<\/span><span style=\"color: #666600;\">(<\/span><span style=\"color: #000000;\">resultCode <\/span><span style=\"color: #666600;\">==<\/span><span style=\"color: #000000;\"> RESULT_OK<\/span><span style=\"color: #666600;\">)<\/span><span style=\"color: #666600;\">{<\/span><span style=\"color: #000000;\">\r\n            <\/span>\/\/ The user picked a contact.<span style=\"color: #000000;\">\r\n            <\/span>\/\/ The Intent's data Uri identifies which contact was selected.<span style=\"color: #000000;\">\r\n\r\n            <\/span>\/\/ Do something with the contact here (bigger example below)<span style=\"color: #000000;\">\r\n        <\/span><span style=\"color: #666600;\">}<\/span><span style=\"color: #000000;\">\r\n    <\/span><span style=\"color: #666600;\">}<\/span><span style=\"color: #000000;\">\r\n<\/span><span style=\"color: #666600;\">}<\/span><\/pre>\n<\/div>\n<div style=\"color: #000000;\"><\/div>\n<div style=\"color: #000000;\">\u5f53\u4f60\u7684app\u5141\u8bb8\u63a5\u53d7\u5176\u5b83app\u7684\u8c03\u7528\u65f6\uff0c\u53ea\u9700\u5728manifest.xml\u76f8\u5e94\u7684&lt;activiy&gt;\u6807\u7b7e\u4e2d\u901a\u8fc7&lt;intent-filter&gt;\u6765\u58f0\u660e\u3002<\/div>\n<div style=\"color: #000000;\">\n<pre style=\"color: #006600;\"><span style=\"color: #000088;\">&lt;activity<\/span><span style=\"color: #882288;\">android:name<\/span><span style=\"color: #666600;\">=<\/span><span style=\"color: #880000;\">\"ShareActivity\"<\/span><span style=\"color: #000088;\">&gt;<\/span><span style=\"color: #000000;\">\r\n    <\/span>&lt;!-- filter for sending text; accepts SENDTO action with sms URI schemes --&gt;<span style=\"color: #000000;\">\r\n    <\/span><span style=\"color: #000088;\">&lt;intent-filter&gt;<\/span><span style=\"color: #000000;\">\r\n        <\/span><span style=\"color: #000088;\">&lt;action<\/span><span style=\"color: #882288;\">android:name<\/span><span style=\"color: #666600;\">=<\/span><span style=\"color: #880000;\">\"android.intent.action.SENDTO\"<\/span><span style=\"color: #000088;\">\/&gt;<\/span><span style=\"color: #000000;\">\r\n        <\/span><span style=\"color: #000088;\">&lt;category<\/span><span style=\"color: #882288;\">android:name<\/span><span style=\"color: #666600;\">=<\/span><span style=\"color: #880000;\">\"android.intent.category.DEFAULT\"<\/span><span style=\"color: #000088;\">\/&gt;<\/span><span style=\"color: #000000;\">\r\n        <\/span><span style=\"color: #000088;\">&lt;data<\/span><span style=\"color: #882288;\">android:scheme<\/span><span style=\"color: #666600;\">=<\/span><span style=\"color: #880000;\">\"sms\"<\/span><span style=\"color: #000088;\">\/&gt;<\/span><span style=\"color: #000000;\">\r\n        <\/span><span style=\"color: #000088;\">&lt;data<\/span><span style=\"color: #882288;\">android:scheme<\/span><span style=\"color: #666600;\">=<\/span><span style=\"color: #880000;\">\"smsto\"<\/span><span style=\"color: #000088;\">\/&gt;<\/span><span style=\"color: #000000;\">\r\n    <\/span><span style=\"color: #000088;\">&lt;\/intent-filter&gt;<\/span><span style=\"color: #000000;\">\r\n    <\/span>&lt;!-- filter for sending text or images; accepts SEND action and text or image data --&gt;<span style=\"color: #000000;\">\r\n    <\/span><span style=\"color: #000088;\">&lt;intent-filter&gt;<\/span><span style=\"color: #000000;\">\r\n        <\/span><span style=\"color: #000088;\">&lt;action<\/span><span style=\"color: #882288;\">android:name<\/span><span style=\"color: #666600;\">=<\/span><span style=\"color: #880000;\">\"android.intent.action.SEND\"<\/span><span style=\"color: #000088;\">\/&gt;<\/span><span style=\"color: #000000;\">\r\n        <\/span><span style=\"color: #000088;\">&lt;category<\/span><span style=\"color: #882288;\">android:name<\/span><span style=\"color: #666600;\">=<\/span><span style=\"color: #880000;\">\"android.intent.category.DEFAULT\"<\/span><span style=\"color: #000088;\">\/&gt;<\/span><span style=\"color: #000000;\">\r\n        <\/span><span style=\"color: #000088;\">&lt;data<\/span><span style=\"color: #882288;\">android:mimeType<\/span><span style=\"color: #666600;\">=<\/span><span style=\"color: #880000;\">\"image\/*\"<\/span><span style=\"color: #000088;\">\/&gt;<\/span><span style=\"color: #000000;\">\r\n        <\/span><span style=\"color: #000088;\">&lt;data<\/span><span style=\"color: #882288;\">android:mimeType<\/span><span style=\"color: #666600;\">=<\/span><span style=\"color: #880000;\">\"text\/plain\"<\/span><span style=\"color: #000088;\">\/&gt;<\/span><span style=\"color: #000000;\">\r\n    <\/span><span style=\"color: #000088;\">&lt;\/intent-filter&gt;<\/span><span style=\"color: #000000;\">\r\n<\/span><span style=\"color: #000088;\">&lt;\/activity&gt;<\/span><\/pre>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>App\u5185\u90e8\u901a\u8fc7\u521b\u5efaIntent\u5e76\u663e\u793a\u7684\u6307\u660e\u9700\u8981start\u7684\u7ec4\u4ef6\u7c7b\u540d\u6765\u5bfc\u822a\u5230\u53e6\u4e00\u4e2aActivity\uff0c\u800c\u5bfc\u822a\u5230\u53e6\u4e00 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.yarpee.cn\/?p=94\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cInteracting with Other Apps\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":[10],"tags":[],"class_list":["post-94","post","type-post","status-publish","format-standard","hentry","category-android"],"_links":{"self":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts\/94"}],"collection":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=94"}],"version-history":[{"count":1,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":95,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions\/95"}],"wp:attachment":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}