Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 原生分享功能的思考與實作

編輯:關於 Android 程式設計

因公司需求,近來一直在做分享功能。大致有幾種思路:一是用 Intent 呼叫 Android 原生分享;二是使用第三方 SDK,如 ShareSDK 或友盟;三是到各平台下載 jar 包、參照官方文件自行實現——這種通常較複雜,尤其微信的接入總讓人費解。這裡選擇最輕量的原生 Intent 方案,直接上代碼。

一、封裝 ShareUtil 工具類

核心思路是構造一個 ACTION_SEND 的 Intent,透過 setPackage 或 setComponent 指定目標應用,再用 Intent.createChooser 彈出分享面板。以下為文字與網頁分享:

public class ShareUtil {
    private Context context;
    public ShareUtil(Context context) {
        this.context = context;
    }

    /** 分享文字 */
    public void shareText(String packageName, String className,
                          String content, String title, String subject) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("text/plain");
        if (stringCheck(className) && stringCheck(packageName)) {
            intent.setComponent(new ComponentName(packageName, className));
        } else if (stringCheck(packageName)) {
            intent.setPackage(packageName);
        }
        intent.putExtra(Intent.EXTRA_TEXT, content);
        if (title != null && !TextUtils.isEmpty(title)) {
            intent.putExtra(Intent.EXTRA_TITLE, title);
        }
        if (subject != null && !TextUtils.isEmpty(subject)) {
            intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        }
        Intent chooserIntent = Intent.createChooser(intent, "分享到:");
        context.startActivity(chooserIntent);
    }
    // shareUrl 與 shareText 邏輯一致
}

圖片、音樂、視頻分享則透過 EXTRA_STREAM 攜帶文件 URI,並依類型設定相應的 MIME:

/** 分享圖片 */
public void shareImg(String packageName, String className, File file) {
    if (file.exists()) {
        Uri uri = Uri.fromFile(file);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("image/*");
        if (stringCheck(packageName) && stringCheck(className)) {
            intent.setComponent(new ComponentName(packageName, className));
        } else if (stringCheck(packageName)) {
            intent.setPackage(packageName);
        }
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        context.startActivity(Intent.createChooser(intent, "分享到:"));
    } else {
        Toast.makeText(context, "文件不存在", 1000).show();
    }
}

/** 分享視頻 */
public void shareVideo(String packageName, String className, File file) {
    setIntent("video/*", packageName, className, file);
}

/** 分享到微信朋友圈 */
public void shareImgToWXCircle(String title, String packageName,
                               String className, File file) {
    if (file.exists()) {
        Uri uri = Uri.fromFile(file);
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName, className));
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.putExtra("Kdescription", title);
        context.startActivity(intent);
    } else {
        Toast.makeText(context, "文件不存在", Toast.LENGTH_LONG).show();
    }
}

/** 檢查是否安裝分享目標 App */
public boolean checkInstall(String packageName) {
    try {
        context.getPackageManager().getPackageInfo(
            packageName, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        Toast.makeText(context, "請先安裝應用 app", 1500).show();
        return false;
    }
}

二、在 Activity 中調用

在 MainActivity 中為各按鈕綁定點擊事件,依傳入的 packageName 與 className 決定分享到哪個應用。例如分享到微信好友,會指定微信的分享 Activity:

switch (v.getId()) {
    case R.id.btn_wx:
        shareUtil.shareText("com.tencent.mm", null,
            "這是一條分享信息", "分享標題", "分享主題");
        break;
    case R.id.btn_wxFriend:
        if (shareUtil.checkInstall("com.tencent.mm")) {
            shareUtil.shareText("com.tencent.mm",
                "com.tencent.mm.ui.tools.ShareImgUI",
                "https://www.aiipu.com/", "分享標題", "分享主題");
        } else {
            shareUtil.toInstallWebView("https://weixin.qq.com/download");
        }
        break;
    case R.id.btn_wxCircle_img:
        shareUtil.shareImgToWXCircle("狗狗圖片", "com.tencent.mm",
            "com.tencent.mm.ui.tools.ShareToTimeLineUI", fileImage);
        break;
    // QQ 與其他分享依此類推
}

需要特別留意的是:微信的分享僅支援「文字+圖片」組合,無法單獨只分享純文字或純圖片。整體而言,原生 Intent 方案無需引入任何第三方 SDK,就能實現分享到微信、QQ 乃至系統分享面板的常見需求,輕量而實用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved