Email模块(1)-图库分享无Email

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sage_wang/article/details/52851844

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系统自带的分享功能使用的隐式启动Activity的方法,这里的Action使用的是ACTION_SENDandroid.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. 应用

在Android系统中如何给应用增加分享功能,怎样将应用加入系统的分享选择列表? Intent.createChooser()方法用来弹出系统分享列表。但是,查看Intent对应的组件是否存在,可查看Android判断Intent是否存在,是否可用,当Android系统调用Intent时,如果没有找到Intent匹配的Activity组件Component,那么应用将报以下错误:
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、应用增加分享功能

若想分享图片信息需要设置setType为“image/*”,传递一个类型为Uri的参数[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、应用加入系统分享列表

只需在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> 

以Gallery的第一个界面,长按一个文件或这文件夹,会弹出分享的按钮,点击分享按钮,会弹出popupWindow,里面列出可以分享的应用,下面详细的分析下这个过程:

  • Gallery SlotView.MyGestureListener.onLongPress();//长按页面,进入长按的调用流程
  • Gallery ActionModeHandler.updateSelectionMenu;//去计算选中的count
  • Gallery ActionModeHandler.updateSupportedOperation().setShowAsAction();//将会触发Acionbar的View的创建和数据的准备
  • Gallery ContainerPage.onCreateActionBar()
  • Gallery GalleryActionBar.createActionBarMenu();
  • Framework ShareActionProvider.onCreateActionView();//创建ActionBar的View,并在ShareActionProvider.java中设置监听事件
  • Framework ActivityChooserModel.Callbacks;//分享Button接下来被点击的时候,将会Callbacks对应的事件处理

在ActivityChooserModel类中的mActivities是满足条件的可以分享的Activity,当mShareActionProvider.setShareIntent(null); 此时分享将会得到响应。不过需要注意,如果这个Intent在这个地方不为null,将会导致mActivity被清空,同时当mActivities.size() == 0时,分享的按钮是无效的。
在ActivityChooserView.java中ActivityChooserModel.loadActivitiesIfNeeded()
会调用ApplicationPackageManager.queryIntentActivities()。

猜你喜欢

转载自blog.csdn.net/sage_wang/article/details/52851844