栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android将内容分享到QQ和微信实例代码

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android将内容分享到QQ和微信实例代码

具体代码如下所示:

package dmpte.sharewechat;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.widget.Toast;
import java.util.List;

public class AndroidShare {
  
  private Context context;
  
  public static int TEXT = 0;
  
  public static int DRAWABLE = 1;
  public AndroidShare(Context context) {
    this.context = context;
  }
  
  public void shareQQFriend(String msgTitle, String msgText, int type,
 Bitmap drawable) {
    shareMsg("com.tencent.mobileqq",
 "com.tencent.mobileqq.activity.JumpActivity", "QQ", msgTitle,
 msgText, type, drawable);
  }
  
  public void shareWeChatFriend(String msgTitle, String msgText, int type,
   Bitmap drawable) {
    shareMsg("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI", "微信",
 msgTitle, msgText, type, drawable);
  }
  
  public void shareWeChatFriendCircle(String msgTitle, String msgText,
      Bitmap drawable) {
    shareMsg("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI",
 "微信", msgTitle, msgText, AndroidShare.DRAWABLE, drawable);
  }
  
  @SuppressLint("NewApi")
  private void shareMsg(String packageName, String activityName,
      String appname, String msgTitle, String msgText, int type,
      Bitmap drawable) {
    if (!packageName.isEmpty() && !isAvilible(context, packageName)) {// 判断APP是否存在
      Toast.makeText(context, "请先安装" + appname, Toast.LENGTH_SHORT)
   .show();
      return;
    }
    Intent intent = new Intent("android.intent.action.SEND");
    if (type == AndroidShare.TEXT) {
      intent.setType("text/plain");
    } else if (type == AndroidShare.DRAWABLE) {
      intent.setType("image
  public boolean isAvilible(Context context, String packageName) {
    PackageManager packageManager = context.getPackageManager();
    List pinfo = packageManager.getInstalledPackages(0);
    for (int i = 0; i < pinfo.size(); i++) {
      if (((PackageInfo) pinfo.get(i)).packageName
   .equalsIgnoreCase(packageName))
 return true;
    }
    return false;
  }
  
  public void sharedQQ(Activity context, Bitmap bitmap){
    Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(
 context.getContentResolver(), BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher), null, null));
    Intent imageIntent = new Intent(Intent.ACTION_SEND);
    imageIntent.setPackage("com.tencent.mobileqq");
    imageIntent.setType("image/*");
    imageIntent.putExtra(Intent.EXTRA_STREAM, uri);
    imageIntent.putExtra(Intent.EXTRA_TEXT,"您的好友邀请您进入天好圈");
    imageIntent.putExtra(Intent.EXTRA_TITLE,"天好圈");
    context.startActivity(imageIntent);
  }
}

然后是使用

public void shareQQ(View view) {
    AndroidShare androidShare = new AndroidShare(this);
    androidShare.shareQQFriend("这是标题", "这是内容", AndroidShare.TEXT, null);
  }

  public void shareWechat(View view) {
    AndroidShare androidShare = new AndroidShare(this);
    androidShare.shareWeChatFriend("这是标题", "这是内容", AndroidShare.TEXT, null);
  }

就是这么简单

总结

以上所述是小编给大家介绍的Android将内容分享到QQ和微信实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/155950.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号