9.1筆記,系統分享,Fragment not attached to Activity
2015-09-01 21:15
375 查看
不用sdk,用系統自帶分享功能
http://ju.outofmemory.cn/entry/172831package 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()
相关文章推荐
- QT中实现类似网页搜索的自动出现下拉提示
- cat proc/version在内核中是如何实现的?
- zepto.js 处理Touch事件(实例)
- Android项目开发实战:倒计时
- 解决VS2010链接错误:LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- 数据结构基础 之 双链表
- 正则表达式
- NOIP2012国王游戏
- createrepo and smart channel
- Oracle数据库修改字段长度
- 采样示波器和实时示波器的原理与各自优势
- C#中的线程(二) 线程同步基础
- 获取硬件因子
- 求递归算法时间复杂度:递归树【待研究】
- C#中的线程(二) 线程同步基础
- 数据结构基础 之 循环链表
- 面试题26:复杂链表复制
- 整数的个数
- 聚簇索引和非聚簇索引
- 《IOS_C语言》函数指针、函数回调、动态排序、函数返回值是函数指针