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;
/**
 * Created by Administrator on 2018/6/25.
 */
public class AndroidShare {
  /**
   * 上下文
   */
  private Context context;
  /**
   * 文本类型
   *
   */
  public static int TEXT = 0;
  /**
   * 图片类型
   */
  public static int DRAWABLE = 1;
  public AndroidShare(Context context) {
    this.context = context;
  }
  /**
   * 分享到QQ好友
   *
   * @param msgTitle
   *      (分享标题)
   * @param msgText
   *      (分享内容)
   * @param type
   *      (分享类型)
   * @param drawable
   *      (分享图片,若分享类型为AndroidShare.TEXT,则可以为null)
   */
  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);
  }
  /**
   * 分享到微信好友
   *
   * @param msgTitle
   *      (分享标题)
   * @param msgText
   *      (分享内容)
   * @param type
   *      (分享类型)
   * @param drawable
   *      (分享图片,若分享类型为AndroidShare.TEXT,则可以为null)
   */
  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);
  }
  /**
   * 分享到微信朋友圈(分享朋友圈一定需要图片)
   *
   * @param msgTitle
   *      (分享标题)
   * @param msgText
   *      (分享内容)
   * @param 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);
  }
  /**
   * 点击分享的代码
   *
   * @param packageName
   *      (包名,跳转的应用的包名)
   * @param activityName
   *      (类名,跳转的页面名称)
   * @param appname
   *      (应用名,跳转到的应用名称)
   * @param msgTitle
   *      (标题)
   * @param msgText
   *      (内容)
   * @param type
   *      (发送类型:text or pic 微信朋友圈只支持pic)
   */
  @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/*");
//     BitmapDrawable bd = (BitmapDrawable) drawable;
//     Bitmap bt = bd.getBitmap();
      final Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(
          context.getContentResolver(), drawable, null, null));
      intent.putExtra(Intent.EXTRA_STREAM, uri);
    }
    intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
    intent.putExtra(Intent.EXTRA_TEXT, msgText);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if (!packageName.isEmpty()) {
      intent.setComponent(new ComponentName(packageName, activityName));
      context.startActivity(intent);
    } else {
      context.startActivity(Intent.createChooser(intent, msgTitle));
    }
  }
  /**
   * 判断相对应的APP是否存在
   *
   * @param context
   * @param packageName
   * @return
   */
  public boolean isAvilible(Context context, String packageName) {
    PackageManager packageManager = context.getPackageManager();
    List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
    for (int i = 0; i < pinfo.size(); i++) {
      if (((PackageInfo) pinfo.get(i)).packageName
          .equalsIgnoreCase(packageName))
        return true;
    }
    return false;
  }
  /**
   * 指定分享到qq
   * @param context
   * @param bitmap
   */
  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和微信实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • Android使用友盟集成QQ、微信、微博等第三方分享与登录方法详解

    最近项目需要加入第三方分享和登录功能,之前其他项目的第三方分享和登录一直都使用ShareSDK实现的.为了统一使用友盟的全家桶,所以三方分享和登录也就选择了友盟.这里记录一下完整的集成与使用流程. 1.申请友盟Appkey 直接到友盟官网申请即可 2.下载SDK 下载地址:http://dev.umeng.com/social/android/sdk-download 下载的时候根据自己需求进行选择,我这里选择选择的是精简版(包含常用的分享与登录功能),只测试微信,QQ,新浪微博. 下载后解压出

  • 分享Android微信红包插件

    本文实例为大家分享了Android微信红包插件,供大家参考,具体内容如下 效果图: 具体代码 @TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void handleWindowChange(AccessibilityNodeInfo nodeInfo) { switch (Stage.getInstance().getCurrentStage()) { case Stage.OPENING_STAGE: // 调试信息,打印TTL // Lo

  • Android调用微信登陆、分享、支付

    用了微信sdk各种痛苦,感觉比qq sdk调用麻烦多了,回调过于麻烦,还必须要在指定包名下的actvity进行回调,所以我在这里写一篇博客,有这个需求的朋友可以借鉴一下,以后自己别的项目有用到也有个找资料的地方. 一.微信登陆分三个步骤: 1).微信授权登陆   2).根据授权登陆code 获取该用户token   3).根据token获取用户资料   4).接收微信的请求及返回值 如果你的程序需要接收微信发送的请求,或者接收发送到微信请求的响应结果,需要下面3步操作: a. 在你的包名相应目录

  • Android实现分享微信好友及出现闪退的解决办法

     1.申请微信APPID 要实现分享到微信的功能,首先要到微信开放平台申请一个APPID.但在申请APPID的时候需要填写一个应用签名和应用包名.需要注意的是包名必须与开发应用时的包名一致,应用签名也必须去掉冒号而且字母为小写. 2.应用签名的获取 开发android应用的人很多,很有可能类名.包名起成了同一个名字,签名这时候就起到区分的作用. 所有的Android应用都必须有数字签名,不存在没有数字签名的应用,包括模拟器运行的.模拟器开发环境,开发时,通过ADB接口上传的程序会自动被签有Deb

  • Android调用第三方QQ登录代码分享

    本文为大家分享了调用QQ登录的相关代码,希望对大家有帮助,减少项目开发的时间,具体内容如下 1.去QQ开放平台注册帐号(http://open.qq.com/),为应用申请QQ的APP_ID , 并下载相关的jar包,放到工程的lib目录下. 2.在Manifest.xml里注册QQ相关的Activity,代码如下 <activity android:name="com.tencent.connect.common.AssistActivity" android:screenOr

  • Android高仿微信聊天界面代码分享

    微信聊天现在非常火,是因其界面漂亮吗,哈哈,也许吧.微信每条消息都带有一个气泡,非常迷人,看起来感觉实现起来非常难,其实并不难.下面小编给大家分享实现代码. 先给大家展示下实现效果图: OK,下面我们来看一下整个小项目的主体结构: 下面是Activity的代码: package com.way.demo; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import jav

  • Android 实现微信,微博,微信朋友圈,QQ分享的功能

    Android 实现微信,微博,微信朋友圈,QQ分享的功能 一.去各自所在的开发者平台注册相应的Key值:引入相关jar包.权限等 二.ShareUtil工具类 import android.app.Activity; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.graphics.Bit

  • Android实现截图分享qq 微信功能

    在上篇文章给大家介绍了Android实现截图和分享功能的代码.感兴趣可以点击阅读,今天通过本文给大家介绍Android实现截图分享qq 微信功能.一起看看吧. 前言 现在很多应用都有截图分享的功能,今天就来讲讲截图分享吧 今天涉及到以下内容: Android截屏 Android分享 效果图展示 ok,下面就来具体讲讲 一.权限,注意权限 先在自己的mainfast中添加以下权限: <uses-permission android:name="android.permission.WRITE

  • 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; impo

  • android输入框内容改变的监听事件实例

    android输入框内容改变的监听事件一般用于比如我们常见的:登录qq时 用户名输入完整时头像自动显示,或者注册用户时实时提示注册格式是否正确等.那么我们在这里举例:判断输入框是否有内容,来改变按钮的状态,常用于搜索一类.截图如下:(布局代码不再给出) 首先所在的activity要 implements TextWatcher并实现其方法: public void afterTextChanged(Editable arg0) { // 文字改变后出发事件 String content = ed

  • Android第三方登录之腾讯QQ登录的实例代码

    布局文件 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录成功" android:textSize="25sp" android:layout_marginTop="100dp" /> 清单文件中的配置 <activity android:n

  • Android 自定义弹出菜单和对话框功能实例代码

    Android 开发当中,可能会存在许多自定义布局的需求,比如自定义弹出菜单(popupWindow),以及自定义对话框(Dialog). 话不多说,直接上图片. 先讲第一种,自定义PopUpWindow 1.popupWindow protected void showPopWindow(View view, final int pos){ WindowManager wm= (WindowManager) myContext.getSystemService(Context.WINDOW_S

  • android获取附近蓝牙设备并计算距离的实例代码

    需要用到本地蓝牙适配器 // 获取本地蓝牙适配器 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 判断是否支持蓝牙,并确认打开该功能. // 判断手机是否支持蓝牙 if (mBluetoothAdapter == null) { Toast.makeText(this, "设备不支持蓝牙", Toast.LENGTH_SHORT).show(); finish(); } // 判断是否打开蓝牙 if (!mBlueto

  • Android中View跟随手指滑动效果的实例代码

    本文讲述了Android中View跟随手指滑动效果的实例代码.分享给大家供大家参考,具体如下: 1.android View 主要6种滑动方法,分别是 layout() offsetLeftAndRight()和offsetTopAndBottom() LayoutParams scrollBy()和 scrollTo() Scroller 动画 2.实现效果图 3.自定义中使用layout()方法实习view的滑动 public class MoveView extends View { pr

  • Android仿美团下拉菜单(商品选购)实例代码

    美团电商应用平台大家使用非常频繁,下面小编通过本文给大家介绍电商应用平台中常用的选择类别下拉列表的实现.先给大家展示下效果图: 一.下拉列表的实现 其实实现方法有很多,这时实现的也没有什么技术含量,只是总结下自己在项目中的做法,也提供一个思路. 首先是列表的数据,一般数据都是从后台读过来,这里因为没有后台,所以写死在客户端: private void initMenuData() { menuData = new ArrayList<map<string, string=""

  • Android 模拟新闻APP显示界面滑动优化实例代码

    内容: 1.滑动优化(滑动时不加载图片,停止才加载) 2.第一次进入时手动加载 代码如下: 1.界面布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:

  • Android 实现会旋转的饼状统计图实例代码

    Android 实现会旋转的饼状统计图实例代码 最近在做一个项目,由于有需要统计的需要,于是就做成了下面饼状统计图. 下图是效果图: 大致思路是: 关于的介绍这里不做详细介绍,如果想深入请点击开源项目MPAndroidChart 下面是其实现: 首先是添加MPAndroidChart依赖: maven { url "https://jitpack.io" } compile 'com.github.PhilJay:MPAndroidChart:v3.0.1' Mainactivity

随机推荐