今天老大出了個需求,要將我們的應用加入到分享列表裡面。做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);