9.1筆記,系統分享,Fragment not attached to Activity

不用sdk,用系統自帶分享功能

http://ju.outofmemory.cn/entry/172831

package zivixgroup.com.skilltemp.utils;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;

import java.io.File;

import zivixgroup.com.skilltemp.Config.PushCode;
import zivixgroup.com.skilltemp.R;

/**
 * Created by george.yang on 2015/8/31.
 */
public class ShareUtil {
    public static boolean shareToEmail (Activity activity,String subject,String content) {
        try {
            Intent email = new Intent(android.content.Intent.ACTION_SEND);
            email.setType("plain/text");
            //i.setType("message/rfc822");
            //邮件主题
            email.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
            //邮件内容
            email.putExtra(android.content.Intent.EXTRA_TEXT, content);

            //请选择邮件发送内容
            activity.startActivityForResult(Intent.createChooser(email, activity.getString(R.string.ChooseSendType)), PushCode.share_email);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 发短信
     */
    public static   boolean  sendSMS(Activity activity,String content){
        try {
            Uri smsToUri = Uri.parse("smsto:");
            Intent sendIntent =  new  Intent(Intent.ACTION_VIEW, smsToUri);
            //sendIntent.putExtra("address", "123456"); // 电话号码,这行去掉的话,默认就没有电话
            //短信内容
            sendIntent.putExtra( "sms_body", content);
            sendIntent.setType("vnd.android-dir/mms-sms");
            activity.startActivityForResult(sendIntent, PushCode.share_sms);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public static boolean shareToWhatsApp (Activity activity,String content) {
        Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
        whatsappIntent.setType("text/plain");
        whatsappIntent.setPackage("com.whatsapp");
        whatsappIntent.putExtra(Intent.EXTRA_TEXT, content);
        try {
            activity.startActivity(whatsappIntent);
        } catch (android.content.ActivityNotFoundException ex) {
           return false;
        }
        return true;
    }

    /**
     * 分享信息到朋友
     * @param activity
     * @return
     */
    public static boolean shareToWeChatFriend (Activity activity,String content) {
        try {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_TEXT, content);
            intent.setType("text/*");

            ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
            intent.setComponent(componentName);
            activity.startActivity(intent);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 分享信息到朋友圈
     *
     */
    public static boolean shareToWeChatTimeLine(Activity activity,String content) {
        try {
            Intent intent = new Intent();
            ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
            intent.setComponent(componentName);

            intent.setAction(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_TEXT, content);
            intent.setType("text/*");
            activity.startActivity(intent);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 分享信息到朋友圈
     *
     * @param file,假如图片的路径为path,那么file = new File(path);
     */
    public static boolean shareToWeChatTimeLine(Activity activity,File file,String content) {
        try {
            Intent intent = new Intent();
            ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
            intent.setComponent(componentName);

            intent.setAction(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            intent.putExtra(Intent.EXTRA_TEXT, content);
            intent.setType("image/*;text/*");
            activity.startActivity(intent);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}

防止
java.lang.IllegalStateException: Fragment not attached to Activity
http://blog.chengyunfeng.com/?p=522
比較多人回答是判斷一下:
isadd()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值