实例介绍
【实例简介】
android崩溃日志保存SDCARD-本地阅读-并发送email
相关配置在configuration_appinit.xml文件
<?xml version="1.0" encoding="utf-8"?> <Application-configuration xmlns:android="http://schemas.android.com/apk/res/android"> <!--是否测试环境--> <IsTestEnvironment value="false" /> <!--是否显示测试Toast--> <IsDebugToast value="true" /> <!--是否显示测试数据--> <IsDebugLog value="true" /> <!--是否使用测试数据--> <IsDebugData value="false" /> <!--是否单元测试--> <IsUnitTest value="true" /> <!--字符编码--> <Charset value="utf-8" /> <!--是否在SdCard上生成日志--> <IsCreateFileLog value="true" /> <!--SdCard上生成日志路径--> <CreateFileLogPath value="\Log\" /> <!--是否在崩溃时掉以html或者方式打开--> <IsOpenSystemCrash value="true" /> <!--是否在崩溃时发送日志到指定邮箱--> <IsSendErrorToEmail value="true" /> <!--发送邮箱的用户名--> <SendMailHostUrl value="smtp.qq.com" /> <!--发送邮箱的用户名--> <SendMailUserName value="" /> <!--发送邮箱的密码--> <SendMailPassWord value="" /> <!--要发送的邮箱--> <ReceiveMailUserName value="" /> </Application-configuration>
【实例截图】
【核心代码】
- package org.lxz.utils.android.debug;
- import <a href="http://lib.csdn.net/base/17" class="replace_word" title="Java EE知识库" target="_blank" style="color: rgb(223, 52, 52); font-weight: bold;">java</a>.lang.Thread.UncaughtExceptionHandler;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import org.lxz.utils.android.info.ApplitionInfo;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.content.pm.ApplicationInfo;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.content.pm.PackageManager.NameNotFoundException;
- import android.graphics.drawable.Drawable;
- import android.util.Log;
- /**
- * 系统崩溃日志
- * @author Aiushtha
- */
- @SuppressLint("SimpleDateFormat")
- public class SystemCrashLog implements UncaughtExceptionHandler,Runnable{
- /**单例*/
- private static SystemCrashLog INSTANCE ;
- /**上下文环境*/
- private Context mContext;
- /**错误*/
- private Throwable ex;
- /**初始化*/
- public static SystemCrashLog init(Context context) {
- return INSTANCE=(INSTANCE==null?new SystemCrashLog(context):INSTANCE);
- }
- /**构造方法*/
- public SystemCrashLog(Context ctx) {
- mContext = ctx;
- Thread.setDefaultUncaughtExceptionHandler(this);
- }
- /**捕获异常并处理*/
- @Override
- public void uncaughtException(Thread thread, final Throwable ex) {
- this.ex=ex;
- LocalLogRunnable localLogRunnable=new LocalLogRunnable(mContext,ex);
- String subject="应用程序" " " "EmailDemo" " " "发生了一个崩溃";
- StringBuffer sb=new StringBuffer();
- sb.append("android-id:" ApplitionInfo.getAndroidId(mContext) "\n")
- .append("android-code:" ApplitionInfo.getVersionCode(mContext) "\n")
- .append("android-version:" ApplitionInfo.getVersionName(mContext) "\n");
- localLogRunnable.run();
- EmailRunnable emailRunnable=new EmailRunnable(mContext,ex);
- emailRunnable.setSubject(subject);
- emailRunnable.setBody(sb.toString());
- emailRunnable.setAttachment(localLogRunnable.getLog_file_path());
- new Thread(emailRunnable).start();;
- }
- public String getApplicationName() {
- PackageManager packageManager = null;
- ApplicationInfo applicationInfo = null;
- try {
- packageManager = mContext.getApplicationContext().getPackageManager();
- applicationInfo = packageManager.getApplicationInfo(mContext.getApplicationContext().getPackageName(), 0);
- } catch (PackageManager.NameNotFoundException e) {
- applicationInfo = null;
- }
- String applicationName =
- (String) packageManager.getApplicationLabel(applicationInfo);
- return applicationName;
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- }
- }
- package org.lxz.utils.android.email;
- import java.io.ByteArrayInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.security.Security;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.util.Properties;
- import javax.activation.DataHandler;
- import javax.activation.DataSource;
- import javax.activation.FileDataSource;
- import javax.mail.Authenticator;
- import javax.mail.BodyPart;
- import javax.mail.Message;
- import javax.mail.Multipart;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- import android.util.Log;
- import android.widget.Toast;
- public class MailSender extends Authenticator {
- private String user;
- private String password;
- private Session session;
- private String mailhost = "smtp.gmail.com";//默认用gmail发送
- private Multipart messageMultipart;
- private Properties properties;
- static {
- Security.addProvider(new JSSEProvider());
- }
- public MailSender(String user, String password) {
- this.user = user;
- this.password = password;
- properties = new Properties();
- properties.setProperty("mail.transport.protocol", "smtp");
- // SMTP服务器地址,如smtp.sina.com.cn
- properties.setProperty("mail.host", mailhost);
- // properties.setProperty("mail.stmp.from", user);
- // properties.put("mail.stmp.from", user);
- properties.put("mail.smtp.auth", "true");
- properties.put("mail.smtp.port", "465");
- properties.put("mail.smtp.socketFactory.port", "465");
- properties.put("mail.smtp.socketFactory.class",
- "javax.net.ssl.SSLSocketFactory");
- properties.put("mail.smtp.socketFactory.fallback", "false");
- properties.setProperty("mail.smtp.quitwait", "false");
- session = Session.getDefaultInstance(properties, this);
- messageMultipart=new MimeMultipart();
- }
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(user, password);
- }
- public synchronized void sendMail(String subject, String body,
- String sender, String recipients,String attachment) throws Exception {
- MimeMessage message = new MimeMessage(session);
- message.setSender(new InternetAddress(sender));//邮件发件人
- // message.setSender(new InternetAddress(sender, false));
- message.setSubject(subject);//邮件主题
- //设置邮件内容
- BodyPart bodyPart=new MimeBodyPart();
- bodyPart.setText(body);
- messageMultipart.addBodyPart(bodyPart);
- // message.setDataHandler(handler);
- //设置邮件附件
- if(attachment!=null){
- bodyPart=new MimeBodyPart();
- DataSource dataSource=new FileDataSource(attachment);
- DataHandler dataHandler=new DataHandler(dataSource);
- bodyPart.setDataHandler(dataHandler);
- bodyPart.setFileName(attachment.substring(attachment.lastIndexOf("/") 1));
- messageMultipart.addBodyPart(bodyPart);
- }
- message.setContent(messageMultipart);
- if (recipients.indexOf(',') > 0)
- //多个联系人
- message.setRecipients(Message.RecipientType.TO,
- InternetAddress.parse(recipients));
- else
- //单个联系人
- message.setRecipient(Message.RecipientType.TO, new InternetAddress(
- recipients));
- Transport.send(message);
- }
- //继承DataSource设置字符编码
- public class ByteArrayDataSource implements DataSource {
- private byte[] data;
- private String type;
- public ByteArrayDataSource(byte[] data, String type) {
- super();
- this.data = data;
- this.type = type;
- }
- public ByteArrayDataSource(byte[] data) {
- super();
- this.data = data;
- }
- public void setType(String type) {
- this.type = type;
- }
- public String getContentType() {
- if (type == null)
- return "application/octet-stream";
- else
- return type;
- }
- public InputStream getInputStream() throws IOException {
- return new ByteArrayInputStream(data);
- }
- public String getName() {
- return "ByteArrayDataSource";
- }
- public OutputStream getOutputStream() throws IOException {
- throw new IOException("Not Supported");
- }
- public PrintWriter getLogWriter() throws SQLException {
- // TODO Auto-generated method stub
- return null;
- }
- public int getLoginTimeout() throws SQLException {
- // TODO Auto-generated method stub
- return 0;
- }
- public void setLogWriter(PrintWriter out) throws SQLException {
- // TODO Auto-generated method stub
- }
- public void setLoginTimeout(int seconds) throws SQLException {
- // TODO Auto-generated method stub
- }
- public boolean isWrapperFor(Class<?> arg0) throws SQLException {
- // TODO Auto-generated method stub
- return false;
- }
- public <T> T unwrap(Class<T> arg0) throws SQLException {
- // TODO Auto-generated method stub
- return null;
- }
- public Connection getConnection() throws SQLException {
- // TODO Auto-generated method stub
- return null;
- }
- public Connection getConnection(String theUsername, String thePassword)
- throws SQLException {
- // TODO Auto-generated method stub
- return null;
- }
- }
- public String getMailhost() {
- return mailhost;
- }
- public void setMailhost(String mailhost) {
- this.mailhost = mailhost;
- properties.setProperty("mail.host", this.mailhost);
- }
- }
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论