Android应用中使用及实现系统“分享”接口实例

所属分类: 软件编程 / Android 阅读数: 34
收藏 0 赞 0 分享

为了应用的推广、传播,很多的应用中都有“分享”功能,一个按钮,点击后会出现短信、微博等等一切实现了分享功能的应用列表。这一篇文章主要介绍怎么调用分享功能和怎么实现分享接口让自己应用出现分享列表中。Android应用中能很方便的完成这些功能,这也正是Android的伟大之处,他能很简单的完成应用之间的沟通以相互整合。

调用分享功能

1、分享文本

分享功能使用的隐式启动Activity的方法,这里的Action使用的是 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); 

效果如下图的图一。

2、改变分享列表标题

使用上面的分享方式分享列表标题为“使用一下内容完成操作”,Android中提供了Intent.createChooser() , 这样能一直显示分享选择列表,并且修改了分享列表标题内容。

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(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); 

使用Intent.createChooser()的好处:

If you callIntent.createChooser() for the intent, Android will always display the chooser. This has some advantages:

  • Even if the user has previously selected a default action for this intent, the chooser will still be displayed.
  • If no applications match, Android displays a system message.
  • You can specify a title for the chooser dialog.

           

分享功能不只是Intent.EXTRA_TEXT,还可以 EXTRA_EMAIL ,EXTRA_CC , EXTRA_BCC  ,EXTRA_SUBJECT . 只需要接受方完成响应数据接受。

3、分享图片

分享功能还支持二进制内容(Binary Content),但是多数是处理的图片,因为shareIntent.setType("image/jpeg")这一项设置了内容类型。可也以是其他类型,需要接受方支持。

Intent shareIntent = new Intent(); 
shareIntent.setAction(Intent.ACTION_SEND); 
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage); 
shareIntent.setType("image/jpeg"); 
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to))); 

4、分享图片列表

分享功能不仅支持单张图片,还支持图片列表,这里还是说的范围太窄了,应该声明不仅仅是图片。

ArrayList<Uri> imageUris = new ArrayList<Uri>(); 
imageUris.add(imageUri1); // Add your image URIs here 
imageUris.add(imageUri2); 
 
Intent shareIntent = new Intent(); 
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); 
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris); 
shareIntent.setType("image/*"); 
startActivity(Intent.createChooser(shareIntent, "Share images to..")); 

实现分享功能

上面说的都是怎么调用分享功能,以下就开始写怎么实现分享功能,让我们的应用也出现在分享列表中。前面也说了分享功能是使用隐式调用Activtiy实现的,Activity需要声明 <intent-filter> 。

声明intent-filter

<activity 
      android:name="com.example.sharedemo.ShareActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
        <action android:name="android.intent.action.SEND" /> 
 
        <category android:name="android.intent.category.DEFAULT" /> 
 
        <data android:mimeType="image/*" /> 
      </intent-filter> 
      <intent-filter> 
        <action android:name="android.intent.action.SEND" /> 
 
        <category android:name="android.intent.category.DEFAULT" /> 
 
        <data android:mimeType="text/plain" /> 
      </intent-filter> 
      <intent-filter> 
        <action android:name="android.intent.action.SEND_MULTIPLE" /> 
 
        <category android:name="android.intent.category.DEFAULT" /> 
 
        <data android:mimeType="image/*" /> 
      </intent-filter> 
    </activity> 

上面声明了三种intent-filter,当然可以更多,这里只是举个例子,

处理接收数据
声明了intent-filter,响应的Activity就要处理响应的数据,示例如下:

public class ShareActivity extends Activity{ 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
     
    // Get intent, action and MIME type 
    Intent intent = getIntent(); 
    String action = intent.getAction(); 
    String type = intent.getType(); 
 
    if (Intent.ACTION_SEND.equals(action) && type != null) { 
      if ("text/plain".equals(type)) { 
        handleSendText(intent); // Handle text being sent 
      } else if (type.startsWith("image/")) { 
        handleSendImage(intent); // Handle single image being sent 
      } 
    } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) { 
      if (type.startsWith("image/")) { 
        handleSendMultipleImages(intent); // Handle multiple images being sent 
      } 
    } else { 
      // Handle other intents, such as being started from the home screen 
    } 
  } 
 
  void handleSendText(Intent intent) { 
    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); 
    String sharedTitle = intent.getStringExtra(Intent.EXTRA_TITLE); 
    if (sharedText != null) { 
      // Update UI to reflect text being shared 
    } 
  } 
 
  void handleSendImage(Intent intent) { 
    Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); 
    if (imageUri != null) { 
      // Update UI to reflect image being shared 
    } 
  } 
 
  void handleSendMultipleImages(Intent intent) { 
    ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); 
    if (imageUris != null) { 
      // Update UI to reflect multiple images being shared 
    } 
  } 
} 

通过声明intent-filter,处理接受到的数据就能完成分享的接收功能。

更多

上面只做了分享功能简单的说明,伴随着Android api的升级,也出现了一些新的完成“分享”功能的方法,比如 ShareActionProvider ,更多请参考

demo下载:demo

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

android byte[] 和short[]转换的方法代码

这篇文章主要介绍了android byte[] 和short[]转换的方法代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取应用程序大小的方法

这篇文章主要介绍了Android获取应用程序大小的方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取其他包的Context实例代码

这篇文章主要介绍了Android获取其他包的Context实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android放大镜的实现代码

这篇文章主要介绍了Android放大镜的实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android 读取Properties配置文件的小例子

这篇文章主要介绍了Android 读取Properties配置文件的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android通讯录开发之删除功能的实现方法

这篇文章主要介绍了Android通讯录开发之删除功能的实现方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享
查看更多