今天老大出了个需求,要将我们的应用加入到分享列表里面。做Iphone的小果果说实现不了,在Android上能做。哎,不说什么了。找资料吧。
首先凭多次的经验,应该是以下步骤。
1,找log,logcat是个好东西,能帮你迅速定位到程序的走向,不光是能找bug哦,有些提示信息能帮你找到一些解决问题的入口点,比如说上次要调用2.2设备的前置摄像头。
2,看官方文档,对这个东西的描述,不要急于找网上的案例。要知道,我们的需求千变万化,网上的案例不见得是符合你的需求的,所以不要乱试一通哈。
3,通过官方的描述,找对应的方法或者在网上搜特定的语句,相信很快就能解决了。
下面说下我实际的解决步骤:
1.这是我在logcat里面翻出来的log,很神奇额。
// 07-27 15:53:08.061: INFO/ActivityManager(2460): Starting activity: Intent { act=android.intent.action.CHOOSER cmp=android/com.android.internal.app.ChooserActivity (has extras) }
// 07-27 15:53:08.846: INFO/ActivityManager(2460): Displayed activity android/com.android.internal.app.ChooserActivity: 771 ms (total 771 ms)
// 07-27 15:53:21.592: INFO/ActivityManager(2460): Starting activity: Intent { act=android.intent.action.SEND typ=image/jpeg flg=0x3000000 cmp=com.sina.weibo/.EditActivity (has extras) }
2.现在就去搜这个intent吧(android.intent.action.CHOOSER)慢慢看不翻译了
Activity Action: Display an activity chooser, allowing the user to pick what they want to before proceeding. This can be used as an alternative to the standard activity picker that is displayed by the system when you try to start an activity with multiple possible matches, with these differences in behavior: You can specify the title that will appear in the activity chooser. The user does not have the option to make one of the matching activities a preferred activity, and all possible activities will always be shown even if one of them is currently marked as the preferred activity. This action should be used when the user will naturally expect to select an activity in order to proceed. An example if when not to use it is when the user clicks on a "mailto:" link. They would naturally expect to go directly to their mail app, so startActivity() should be called directly: it will either launch the current preferred app, or put up a dialog allowing the user to pick an app to use and optionally marking that as preferred. In contrast, if the user is selecting a menu item to send a picture they are viewing to someone else, there are many different things they may want to do at this point: send it through e-mail, upload it to a web service, etc. In this case the CHOOSER action should be used, to always present to the user a list of the things they can do, with a nice title given by the caller such as "Send this photo with:". As a convenience, an Intent of this form can be created with the createChooser(Intent, CharSequence) function.
3.好吧,现在搜索吧,关键字: Android 分享
Intent intent=new Intent(Intent.ACTION_SEND); 基本上都有这个语句,好,再搜这个吧。找个几次基本上就能找到了
贴代码吧。
<activity android:name=".Writer_Blog" android:label="share to weibo"> <intent-filter> <action android:name="android.intent.action.SEND"> </action> <category android:name="android.intent.category.DEFAULT"> </category> <data android:mimeType="image/*">//这里是要接收send的类型 </data> </intent-filter> </activity>
这是我自己的微博应用。label是要弹出来的分享列表的文字。<action android:name="android.intent.action.SEND"> 这个就是Intent.ACTION_SEND了。
Intent it = getIntent(); if (it != null && it.getAction() != null && it.getAction().equals(Intent.ACTION_SEND)) { Bundle extras = it.getExtras(); if (extras.containsKey("android.intent.extra.STREAM")) { Log.i(TAG, "uri++=" + extras.get("android.intent.extra.STREAM")); uri = (Uri) extras.get("android.intent.extra.STREAM"); set_image(uri);//这里是将我们所选的分享图片加载出来 } // 07-27 17:39:44.073: INFO/Writer_Blog(3289): android.intent.extra.STREAM=content://media/external/images/media/1211 }
网上找到的代码,能获取所有manifest文件里面加了<action android:name="android.intent.action.SEND"> apps
public List<ResolveInfo> getShareTargets(){ List<ResolveInfo> mApps = new ArrayList<ResolveInfo>(); Intent intent=new Intent(Intent.ACTION_SEND,null); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setType("text/plain"); PackageManager pm=this.getPackageManager(); mApps=pm.queryIntentActivities(intent,PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); return mApps; }
还有一个跳转进市场的代码
Intent intent = new Intent(Intent.ACTION_VIEW); 2 intent.setData(Uri.parse("market://details?id=" + getPackageName())); 3 startActivity(intent);
跳转进市场搜索的代码
Intent intent = new Intent(Intent.ACTION_VIEW); 2 intent.setData(Uri.parse("market://search?q=pub:Your Publisher Name")); 3 startActivity(intent);