1. 解決方法
此為Android5.0 Email的新design,在沒有登入任何account時,不能使用email分享。請參考AccountReconciler類中reconcileAccountsInternal()方法。
//modified by HQ_wangshiqing for AL1016-447 at 20161018 begin
//context.getPackageManager().setComponentEnabledSetting(componentName,
//enableCompose ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
//PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
//PackageManager.DONT_KILL_APP);
context.getPackageManager().setComponentEnabledSetting(componentName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
//modified by HQ_wangshiqing for AL1016-447 at 20161018 end```
### 2. 分析
Android系統(tǒng)自帶的分享功能使用的隱式啟動Activity的方法,這里的Action使用的是*ACTION_SEND*(**android.intent.action.SEND**)
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);```
3. 應(yīng)用
在Android系統(tǒng)中如何給應(yīng)用增加分享功能,怎樣將應(yīng)用加入系統(tǒng)的分享選擇列表? Intent.createChooser()方法用來彈出系統(tǒng)分享列表。但是,查看Intent對應(yīng)的組件是否存在,可查看Android判斷Intent是否存在,是否可用,當(dāng)Android系統(tǒng)調(diào)用Intent時,如果沒有找到Intent匹配的Activity組件Component,那么應(yīng)用將報以下錯誤: android.content.ActivityNotFoundException: Unable to find explicit activity class。所以在使用之前必須判斷一下,代碼如下:
public static boolean intentIsAvailable(Context context, Intent intent) {
final PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);
return list.size() > 0;
} ```
####3.1、應(yīng)用
增加分享功能 若想分享圖片信息需要設(shè)置setType為“image/*”,傳遞一個類型為Uri的參數(shù)[Intent.EXTRA_STREAM]()。
public static void shareText(Context context, String title, String text) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, title);
intent.putExtra(Intent.EXTRA_TEXT, text);
context.startActivity(Intent.createChooser(intent, title));
}
####3.2、應(yīng)用加入系統(tǒng)分享列表只需在AndroidManifest.xml中加入以下代碼:
<activity android:name=".ShareActivity" android:label="分享到初見">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity> ```
4. Gallery 相冊分享底層實現(xiàn)
以Gallery的第一個界面,長按一個文件或這文件夾,會彈出分享的按鈕,點擊分享按鈕,會彈出popupWindow,里面列出可以分享的應(yīng)用,下面詳細的分析下這個過程:
- Gallery SlotView.MyGestureListener.onLongPress();//長按頁面,進入長按的調(diào)用流程 - Gallery ActionModeHandler.updateSelectionMenu;//去計算選中的count
- Gallery ActionModeHandler.updateSupportedOperation().setShowAsAction();//將會觸發(fā)Acionbar的View的創(chuàng)建和數(shù)據(jù)的準備
- ...
- Gallery ContainerPage.onCreateActionBar()
- Gallery GalleryActionBar.createActionBarMenu();
- Framework ShareActionProvider.onCreateActionView();//創(chuàng)建ActionBar的View,并在ShareActionProvider.java中設(shè)置監(jiān)聽事件
- Framework ActivityChooserModel.Callbacks;//分享Button接下來被點擊的時候,將會Callbacks對應(yīng)的事件處理
在ActivityChooserModel類中的mActivities是滿足條件的可以分享的Activity,當(dāng)mShareActionProvider.setShareIntent(null); 此時分享將會得到響應(yīng)。不過需要注意,如果這個Intent在這個地方不為null,將會導(dǎo)致mActivity被清空,同時當(dāng)mActivities.size() == 0時,分享的按鈕是無效的。
在ActivityChooserView.java中ActivityChooserModel.loadActivitiesIfNeeded()
會調(diào)用ApplicationPackageManager.queryIntentActivities()。