国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android將內容分享到QQ和微信實例代碼

2019-10-21 21:49:40
字體:
來源:轉載
供稿:網友

具體代碼如下所示:

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和微信實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石景山区| 岐山县| 资溪县| 郎溪县| 竹北市| 山阳县| 当雄县| 仙桃市| 禄劝| 阿拉善左旗| 建平县| 堆龙德庆县| 兰考县| 辉县市| 永新县| 卓资县| 施秉县| 禹州市| 新绛县| 桑日县| 淮安市| 游戏| 建瓯市| 独山县| 临海市| 永兴县| 琼中| 吴堡县| 定襄县| 灵石县| 庆阳市| 平罗县| 宜宾市| 济宁市| 尉氏县| 永州市| 同德县| 金阳县| 南溪县| 宜丰县| 尼玛县|