Android將內(nèi)容分享到QQ和微信實(shí)例代碼
具體代碼如下所示:
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;
/**
* 文本類(lèi)型
*
*/
public static int TEXT = 0;
/**
* 圖片類(lèi)型
*/
public static int DRAWABLE = 1;
public AndroidShare(Context context) {
this.context = context;
}
/**
* 分享到QQ好友
*
* @param msgTitle
* (分享標(biāo)題)
* @param msgText
* (分享內(nèi)容)
* @param type
* (分享類(lèi)型)
* @param drawable
* (分享圖片,若分享類(lèi)型為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
* (分享標(biāo)題)
* @param msgText
* (分享內(nèi)容)
* @param type
* (分享類(lèi)型)
* @param drawable
* (分享圖片,若分享類(lèi)型為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
* (分享標(biāo)題)
* @param msgText
* (分享內(nèi)容)
* @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);
}
/**
* 點(diǎn)擊分享的代碼
*
* @param packageName
* (包名,跳轉(zhuǎn)的應(yīng)用的包名)
* @param activityName
* (類(lèi)名,跳轉(zhuǎn)的頁(yè)面名稱(chēng))
* @param appname
* (應(yīng)用名,跳轉(zhuǎn)到的應(yīng)用名稱(chēng))
* @param msgTitle
* (標(biāo)題)
* @param msgText
* (內(nèi)容)
* @param type
* (發(fā)送類(lèi)型: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, "請(qǐng)先安裝" + 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));
}
}
/**
* 判斷相對(duì)應(yīng)的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,"您的好友邀請(qǐng)您進(jìn)入天好圈");
imageIntent.putExtra(Intent.EXTRA_TITLE,"天好圈");
context.startActivity(imageIntent);
}
}
然后是使用
public void shareQQ(View view) {
AndroidShare androidShare = new AndroidShare(this);
androidShare.shareQQFriend("這是標(biāo)題", "這是內(nèi)容", AndroidShare.TEXT, null);
}
public void shareWechat(View view) {
AndroidShare androidShare = new AndroidShare(this);
androidShare.shareWeChatFriend("這是標(biāo)題", "這是內(nèi)容", AndroidShare.TEXT, null);
}
就是這么簡(jiǎn)單
總結(jié)
以上所述是小編給大家介紹的Android將內(nèi)容分享到QQ和微信實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android應(yīng)用開(kāi)發(fā)工程目錄作用介紹
這篇文章主要介紹了Android應(yīng)用開(kāi)發(fā)工程目錄作用介紹,本文對(duì)Android項(xiàng)目中的每個(gè)目錄的作用作了總結(jié),需要的朋友可以參考下2014-10-10
Android自定義view實(shí)現(xiàn)仿抖音點(diǎn)贊效果
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)仿抖音點(diǎn)贊效果,代碼簡(jiǎn)單易懂非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05
Android底部導(dǎo)航欄的三種風(fēng)格實(shí)現(xiàn)
這篇文章主要介紹了Android底部導(dǎo)航欄的三種風(fēng)格實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Android實(shí)現(xiàn)手勢(shì)密碼功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手勢(shì)密碼功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android IPC進(jìn)程間通信詳解最新AndroidStudio的AIDL操作)
這篇文章主要介紹了Android IPC進(jìn)程間通信的相關(guān)資料,需要的朋友可以參考下2016-09-09
Android中使用Camera類(lèi)編寫(xiě)手機(jī)拍照App的實(shí)例教程
這篇文章主要介紹了Android中使用Camera類(lèi)編寫(xiě)手機(jī)拍照App的實(shí)例教程,整理了Camera調(diào)用硬件進(jìn)行拍照的一些常用方法,需要的朋友可以參考下2016-04-04
AndroidStudio 使用過(guò)程中出現(xiàn)的異常(Gradle sync failed)處理辦法
本文主要介紹AndroidStudio 使用過(guò)程中出現(xiàn)的異常的解決辦法,這里幫大家舉例說(shuō)明,如何處理出現(xiàn)這種問(wèn)題,有需要的小伙伴可以參考下2016-09-09
Android編程實(shí)現(xiàn)切換imageView的方法分析
這篇文章主要介紹了Android編程實(shí)現(xiàn)切換imageView的方法,結(jié)合具體實(shí)例形式分析了切換imageView的相關(guān)設(shè)置技巧與注意事項(xiàng),需要的朋友可以參考下2017-09-09

