当前位置: 移动技术网 > 移动技术>移动开发>Android > Android应用中使用及实现系统“分享”接口实例

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

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

调用分享功能

1、分享文本

分享功能使用的隐式启动activity的方法,这里的action使用的是 。

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 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,还可以 ,, ,. 只需要接受方完成响应数据接受。

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的升级,也出现了一些新的完成“分享”功能的方法,比如 。

demo下载:

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网