加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Android调用系统分享到Facebook,Messager,WhatsApp

(2023-09-24 01:48:45)
分类: androidios
Android  调用系统分享到其他包中,首先判断手机中是否含有该app

    public static synchronized boolean isContainPackName(final Context mContext , String packName) {
       boolean isContainPack = false;
       try {
          PackageManager packageManager = mContext.getPackageManager();
          PackageInfo info = packageManager.getPackageInfo(packName , PackageManager.GET_ACTIVITIES);
          if(info != null){
             isContainPack = true;
          }
       } catch (PackageManager.NameNotFoundException e) {
          e.printStackTrace();
       }
       return isContainPack;
    }

Facebook 包名为 :   com.facebook.katana

Messager包名为 :    com.facebook.orca

What's App 包名为 :  com.whatsapp

然后调用系统的分享

1.分享文字或链接:

sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,shareText);//shareText 为需要分享的内容
sendIntent.setType("text/plain");
sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.setPackage(packageName);//packageName 为需要分享到的包名
startActivity(sendIntent);

2.分享文件:

 //直接调用系统的分享,我这里只是实现了录音的
    public void shareAudioRecord(Context mContext , File file){
        if(file.exists() && file.length() > 0){
//            Uri mUri = getFileUri(mContext , file);
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addCategory("android.intent.category.DEFAULT");
            intent.putExtra(Intent.EXTRA_STREAM,getFileUri(mContext , file));
            intent.setType("audio*";
//            }
//            intent.setDataAndType(mUri, "audio/3gpp");
            intent.setType("audio/*");
            try {
                mContext.startActivity(Intent.createChooser(intent,mContext.getResources().
                        getString(R.string.shareRecord_titleName)));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static Uri getFileUri(Context context, File file){
        Uri uri;
        // 低版本直接用 Uri.fromFile
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            uri = Uri.fromFile(file);
        }else {
            //  使用 FileProvider 会在某些 app 下不支持(在使用FileProvider 方式情况下QQ不能支持图片、视频分享,微信不支持视频分享)
            uri = FileProvider.getUriForFile(context,
                    BuildConfig.APPLICATION_ID + ".fileProvider",
                    file);

        }
        return uri;
    }

 
————————————————
版权声明:本文为CSDN博主「qb标」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_35041057/article/details/88722832

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有