不采用第三方直接使用Intent分享,主要都是针对国外的比如FB,Line,WhatsApp,Telegram

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/maz5275125/article/details/77162985

其实系统的分享很简单,我们只要找到想要分享到的APP中的Action在加上知道包名就可以了


首先检查手机是否安装了某一项APP


public static boolean isAPPInstalled(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    List<PackageInfo> pinfo = pm.getInstalledPackages(0);
    for (int i = 0; i < pinfo.size(); i++) {
        if (pinfo.get(i).packageName.equals(packageName)) {
            return true;
        }
    }
    return false;
}

我们只要传递包名就可以了


分享代码基本都千篇一律


/**
 * line分享
 */
public void lineShare(String url, String content) {
    try {
        ComponentName cn = new ComponentName("jp.naver.line.android", "jp.naver.line.android.activity.selectchat.SelectChatActivity");
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("text/plain"); // 纯文本
        shareIntent.putExtra(Intent.EXTRA_TEXT, content + " " + url);
        shareIntent.setComponent(cn);//跳到指定APP的Activity
        mActivity.startActivity(Intent.createChooser(shareIntent, ""));
    } catch (Exception ex) {
        Logger.e(TAG, "lineShare:" + ex);
    }
}

public void whatsAppShare(String url, String content) {
    try {
        Intent vIt = new Intent("android.intent.action.SEND");
        vIt.setPackage("com.whatsapp");
        vIt.setType("text/plain");
        vIt.putExtra(Intent.EXTRA_TEXT, content + " " + url);
        mActivity.startActivity(vIt);
    } catch (Exception ex) {
        Logger.e(TAG, "whatsAppShare:" + ex);
    }
}

public void messengerShare(String url, String content) {
    try {
        Intent vIt = new Intent("android.intent.action.SEND");
        vIt.setPackage("com.facebook.orca");
        vIt.setType("text/plain");
        vIt.putExtra(Intent.EXTRA_TEXT, content + " " + url);
        mActivity.startActivity(vIt);
    } catch (Exception ex) {
        Logger.e(TAG, "messengerShare:" + ex);
    }
}

public void telegramShare(String url, String content) {
    try {
        Intent vIt = new Intent("android.intent.action.SEND");
        vIt.setPackage("org.telegram.messenger");
        vIt.setType("text/plain");
        vIt.putExtra(Intent.EXTRA_TEXT, content + " " + url);
        mActivity.startActivity(vIt);
    } catch (Exception ex) {
        Logger.e(TAG, "telegramShare:" + ex);
    }
}

public void wechatShare(String url, String content) {
    try {
        Intent vIt = new Intent("android.intent.action.SEND");
        vIt.setPackage("com.tencent.mm");
        vIt.setType("text/plain");
        vIt.putExtra(Intent.EXTRA_TEXT, content + " " + url);
        mActivity.startActivity(vIt);
    } catch (Exception ex) {
        Logger.e(TAG, "wechatShare:" + ex);
    }
}


很简单吧,就是有的时候包名不是太好找


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页