不用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()