当前位置: 移动技术网 > 移动技术>移动开发>Android > android系统分享的自定义功能的示例代码

android系统分享的自定义功能的示例代码

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

分享功能是app中特别常见的功能,国内的app基本都支持分享到微信 qq等主流的社交应用。至于分享功能的实现大多是使用第三方的share sdk一步到位,或者分享的app比较少比如就一个微信 那通常使用微信sdk的分享模块即可。但其实android系统就给我们提供过一种分享的实现方式,代码也比较简单如下

    intent share = new intent(intent.action_send);
    share.settype("text/plain");
    share.putextra(intent.extra_text, sharetext);
    share.putextra(intent.extra_subject, sharesubject);

    if (uri != null) {
      share.settype("image/*");
      share.putextra(intent.extra_stream, uri);
    }
    context.startactivity(intent.createchooser(share, title));

系统提供的短短不到十行代码,将分享列表 数据 展示 点击 跳转 跳转后分享内容的分享等一系列动作都集合完成了。这样确实给人干净利索的感觉,但随之问题也来了比如我分享列表中只有特定几个app,甚至把某个app放在第一个,还有点击facebook的分享后分享方式我想用facebooksdk自带的,等等一些列自定义功能完成就比较麻烦。

对个别app的分享特别处理

从以上的代码可以看出,google官方定义出分享的key value一一对应规则。比如intent.extra_stream对应为分享图片的uri,intent.extra_text对应为分享的text文本。从道理上讲如果分享到的app都遵循google定义的这规则我们就能通过官方这代码实现分享到所有app的功能。然而理想很丰满现实很骨感,比如我们项目中要求分享到的facebook就压根不遵守这规则,我们想实现分享到facebook就必须用其sdk实现。于是我们就需要在分享列表中点击facebook是单独走facebook的分享逻辑。

  • 常规思维这是一个列表,我们监听列表item的点击事件即可,然而从实现该分享列表的代码 可以看出没有类似listview recyclerview控件,也没有adapter,扒了下源码和google找不到item的点击事件的监听,故该方案放弃了
  • 所以只能采用了自己实现分享列表,然后监听分享列表item点击事件单独处理的方式,也是符合常规思路

第一步:获取分享列表的数据

 

    private static list<list<resolveinfo>> getshareactivities() {
    intent sharingintent = new intent(android.content.intent.action_send);
    sharingintent.settype("text/plain");
    packagemanager pm = app.getinstance().getpackagemanager();
    list<list<resolveinfo>> listarraylist = new arraylist<>();
    list<resolveinfo> activitylist = pm.queryintentactivities(sharingintent, 0);
    list<resolveinfo> newactivitylist = new arraylist<>();

    for (iterator<resolveinfo> it = activitylist.iterator(); it.hasnext(); ) {
      resolveinfo info = it.next();
      //过滤出facebook google+ whatapp twitter 分享app单独处理
      logutils.e("+++", info.activityinfo.packagename);
      if (info.activityinfo.packagename.equals("com.android.bluetooth") || info.activityinfo.packagename.equals("com.android.nfc") || info.activityinfo.packagename.equals("com.facebook.katana") || info.activityinfo.packagename.equals("com.google.android.apps.plus") || info.activityinfo.packagename.equals("com.facebook.orca") || info.activityinfo.packagename.contains("whatsapp") || info.activityinfo.packagename.equals("com.twitter.android")) {
        if (info.activityinfo.packagename.equals("com.android.bluetooth") || info.activityinfo.packagename.equals("com.android.nfc")) {
          it.remove();
        } else {
          newactivitylist.add(info);
          it.remove();
        }
      }
    }
    //增加一条other数据
    newactivitylist.add(null);
    listarraylist.add(newactivitylist);
    listarraylist.add(activitylist);
    return listarraylist;
  }

第二步:构建分享的列表,且定义点击事件等

public static void systemsharedialog(list<resolveinfo> packages, context context, string title, string sharetext) {
    list<intent> targetintents = new arraylist<intent>();
    intent shareintent = new intent(intent.action_send);
    shareintent.settype("text/plain");
    spm spm = new spm();

    for (resolveinfo candidate : packages) {
      string packagename = candidate.activityinfo.packagename;
      intent target = new intent(android.content.intent.action_send);
      target.settype("text/plain");
      target.putextra(intent.extra_text, sharetext);
      //target.setpackage(packagename);
      //t will be able to handle the case of multiple activities within the same app that can handle this intent. otherwise, a weird item of "android system" will be shown
      target.setcomponent(new componentname(packagename, candidate.activityinfo.name));

      targetintents.add(target);
    }
//    createchooser时使用targetintents.remove(0)即传入targetintents的第一个intent,并将其移除,
//
//    否则执行chooser.putextra(intent.extra_initial_intents, targetintents.toarray(new parcelable[] {}));添加后启动时会出现两个相同的应用
    intent chooserintent = intent.createchooser(targetintents.remove(0), title);
    chooserintent.putextra(intent.extra_initial_intents, targetintents.toarray(new parcelable[]{}));
    context.startactivity(chooserintent);

  }

总结

至此完成了基于android系统提供的分享方式上的扩展改造,自定义分享列表数据展示 自定义点击后的分享形式等等都可以实现。。其实此功能中intent.createchooser所做工作还是蛮多的,有兴趣童鞋可以再去扒下起源码,看具体实现和到底做了哪些工作。

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

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

相关文章:

验证码:
移动技术网