在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → android崩溃日志保存SDCARD-本地阅读-并发送email

android崩溃日志保存SDCARD-本地阅读-并发送email

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:3.05M
  • 下载次数:11
  • 浏览次数:211
  • 发布时间:2016-04-12
  • 实例类别:Android平台开发
  • 发 布 人:1265167622
  • 文件格式:.zip
  • 所需积分:2
 相关标签: Email 日志 Android sdcard 阅读

实例介绍

【实例简介】

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>
	


【实例截图】

【核心代码】


  1. package org.lxz.utils.android.debug;  
  2.   
  3. 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;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.ArrayList;  
  6. import java.util.Date;  
  7. import java.util.List;  
  8.   
  9. import org.lxz.utils.android.info.ApplitionInfo;  
  10.   
  11. import android.annotation.SuppressLint;  
  12. import android.content.Context;  
  13. import android.content.pm.ApplicationInfo;  
  14. import android.content.pm.PackageInfo;  
  15. import android.content.pm.PackageManager;  
  16. import android.content.pm.PackageManager.NameNotFoundException;  
  17. import android.graphics.drawable.Drawable;  
  18. import android.util.Log;  
  19.   
  20.   
  21. /** 
  22.  * 系统崩溃日志 
  23.  * @author Aiushtha 
  24.  */  
  25. @SuppressLint("SimpleDateFormat")  
  26. public class SystemCrashLog implements UncaughtExceptionHandler,Runnable{  
  27.       
  28.       
  29.     /**单例*/  
  30.     private static SystemCrashLog INSTANCE ;  
  31.     /**上下文环境*/  
  32.     private Context mContext;  
  33.     /**错误*/  
  34.     private Throwable ex;  
  35.   
  36.     /**初始化*/  
  37.     public static SystemCrashLog init(Context context) {  
  38.         return INSTANCE=(INSTANCE==null?new SystemCrashLog(context):INSTANCE);  
  39.     }  
  40.     /**构造方法*/  
  41.     public SystemCrashLog(Context ctx) {  
  42.         mContext = ctx;  
  43.         Thread.setDefaultUncaughtExceptionHandler(this);  
  44.     }  
  45.     /**捕获异常并处理*/  
  46.     @Override  
  47.     public void uncaughtException(Thread thread, final Throwable ex) {  
  48.         this.ex=ex;  
  49.         LocalLogRunnable  localLogRunnable=new LocalLogRunnable(mContext,ex);  
  50.   
  51.           
  52.         String subject="应用程序" " " "EmailDemo" " " "发生了一个崩溃";  
  53.         StringBuffer sb=new StringBuffer();  
  54.         sb.append("android-id:" ApplitionInfo.getAndroidId(mContext) "\n")  
  55.         .append("android-code:" ApplitionInfo.getVersionCode(mContext) "\n")  
  56.         .append("android-version:" ApplitionInfo.getVersionName(mContext) "\n");  
  57.   
  58.           
  59.         localLogRunnable.run();  
  60.           
  61.         EmailRunnable emailRunnable=new EmailRunnable(mContext,ex);  
  62.         emailRunnable.setSubject(subject);  
  63.         emailRunnable.setBody(sb.toString());  
  64.         emailRunnable.setAttachment(localLogRunnable.getLog_file_path());  
  65.         new Thread(emailRunnable).start();;  
  66.     }  
  67.   
  68.     public String getApplicationName() {   
  69.         PackageManager packageManager = null;   
  70.         ApplicationInfo applicationInfo = null;   
  71.         try {   
  72.             packageManager = mContext.getApplicationContext().getPackageManager();   
  73.             applicationInfo = packageManager.getApplicationInfo(mContext.getApplicationContext().getPackageName(), 0);   
  74.         } catch (PackageManager.NameNotFoundException e) {   
  75.             applicationInfo = null;   
  76.         }   
  77.         String applicationName =    
  78.         (String) packageManager.getApplicationLabel(applicationInfo);   
  79.         return applicationName;   
  80.     }  
  81.     @Override  
  82.     public void run() {  
  83.         // TODO Auto-generated method stub  
  84.           
  85.     }   
  86.    
  87.        
  88.   
  89.   
  90.   
  91. }  
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. package org.lxz.utils.android.email;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.OutputStream;  
  7. import java.io.PrintWriter;  
  8. import java.security.Security;  
  9. import java.sql.Connection;  
  10. import java.sql.SQLException;  
  11. import java.util.Properties;  
  12.   
  13. import javax.activation.DataHandler;  
  14. import javax.activation.DataSource;  
  15. import javax.activation.FileDataSource;  
  16. import javax.mail.Authenticator;  
  17. import javax.mail.BodyPart;  
  18. import javax.mail.Message;  
  19. import javax.mail.Multipart;  
  20. import javax.mail.PasswordAuthentication;  
  21. import javax.mail.Session;  
  22. import javax.mail.Transport;  
  23. import javax.mail.internet.InternetAddress;  
  24. import javax.mail.internet.MimeBodyPart;  
  25. import javax.mail.internet.MimeMessage;  
  26. import javax.mail.internet.MimeMultipart;  
  27.   
  28. import android.util.Log;  
  29. import android.widget.Toast;  
  30.   
  31. public class MailSender extends Authenticator {  
  32.   
  33.     private String user;  
  34.     private String password;  
  35.     private Session session;  
  36.     private String mailhost = "smtp.gmail.com";//默认用gmail发送  
  37.     private Multipart messageMultipart;  
  38.     private Properties properties;  
  39.     static {  
  40.         Security.addProvider(new JSSEProvider());  
  41.     }  
  42.   
  43.     public MailSender(String user, String password) {  
  44.         this.user = user;  
  45.         this.password = password;  
  46.   
  47.         properties = new Properties();  
  48.           
  49.         properties.setProperty("mail.transport.protocol""smtp");  
  50. //      SMTP服务器地址,如smtp.sina.com.cn  
  51.         properties.setProperty("mail.host", mailhost);  
  52.           
  53.           
  54. //      properties.setProperty("mail.stmp.from", user);  
  55. //      properties.put("mail.stmp.from", user);  
  56.           
  57.         properties.put("mail.smtp.auth""true");  
  58.         properties.put("mail.smtp.port""465");  
  59.         properties.put("mail.smtp.socketFactory.port""465");  
  60.         properties.put("mail.smtp.socketFactory.class",  
  61.                 "javax.net.ssl.SSLSocketFactory");  
  62.         properties.put("mail.smtp.socketFactory.fallback""false");  
  63.         properties.setProperty("mail.smtp.quitwait""false");  
  64.           
  65.           
  66.   
  67.         session = Session.getDefaultInstance(properties, this);  
  68.         messageMultipart=new MimeMultipart();  
  69.     }  
  70.   
  71.     protected PasswordAuthentication getPasswordAuthentication() {  
  72.         return new PasswordAuthentication(user, password);  
  73.     }  
  74.   
  75.     public synchronized void sendMail(String subject, String body,  
  76.             String sender, String recipients,String attachment) throws Exception {  
  77.         MimeMessage message = new MimeMessage(session);  
  78.         message.setSender(new InternetAddress(sender));//邮件发件人  
  79. //      message.setSender(new InternetAddress(sender, false));  
  80.         message.setSubject(subject);//邮件主题  
  81.         //设置邮件内容  
  82.         BodyPart bodyPart=new MimeBodyPart();  
  83.         bodyPart.setText(body);  
  84.         messageMultipart.addBodyPart(bodyPart);  
  85. //      message.setDataHandler(handler);  
  86.           
  87.         //设置邮件附件  
  88.         if(attachment!=null){  
  89.             bodyPart=new MimeBodyPart();  
  90.             DataSource dataSource=new FileDataSource(attachment);  
  91.             DataHandler dataHandler=new DataHandler(dataSource);  
  92.             bodyPart.setDataHandler(dataHandler);  
  93.             bodyPart.setFileName(attachment.substring(attachment.lastIndexOf("/") 1));  
  94.             messageMultipart.addBodyPart(bodyPart);  
  95.         }  
  96.         message.setContent(messageMultipart);  
  97.         if (recipients.indexOf(',') > 0)  
  98.             //多个联系人  
  99.             message.setRecipients(Message.RecipientType.TO,  
  100.                     InternetAddress.parse(recipients));  
  101.         else  
  102.             //单个联系人  
  103.             message.setRecipient(Message.RecipientType.TO, new InternetAddress(  
  104.                     recipients));  
  105.         Transport.send(message);  
  106.     }  
  107.   
  108.     //继承DataSource设置字符编码  
  109.     public class ByteArrayDataSource implements DataSource {  
  110.         private byte[] data;  
  111.         private String type;  
  112.   
  113.         public ByteArrayDataSource(byte[] data, String type) {  
  114.             super();  
  115.             this.data = data;  
  116.             this.type = type;  
  117.         }  
  118.   
  119.         public ByteArrayDataSource(byte[] data) {  
  120.             super();  
  121.             this.data = data;  
  122.         }  
  123.   
  124.         public void setType(String type) {  
  125.             this.type = type;  
  126.         }  
  127.   
  128.         public String getContentType() {  
  129.             if (type == null)  
  130.                 return "application/octet-stream";  
  131.             else  
  132.                 return type;  
  133.         }  
  134.   
  135.         public InputStream getInputStream() throws IOException {  
  136.             return new ByteArrayInputStream(data);  
  137.         }  
  138.   
  139.         public String getName() {  
  140.             return "ByteArrayDataSource";  
  141.         }  
  142.   
  143.         public OutputStream getOutputStream() throws IOException {  
  144.             throw new IOException("Not Supported");  
  145.         }  
  146.   
  147.         public PrintWriter getLogWriter() throws SQLException {  
  148.             // TODO Auto-generated method stub  
  149.             return null;  
  150.         }  
  151.   
  152.         public int getLoginTimeout() throws SQLException {  
  153.             // TODO Auto-generated method stub  
  154.             return 0;  
  155.         }  
  156.   
  157.         public void setLogWriter(PrintWriter out) throws SQLException {  
  158.             // TODO Auto-generated method stub  
  159.   
  160.         }  
  161.   
  162.         public void setLoginTimeout(int seconds) throws SQLException {  
  163.             // TODO Auto-generated method stub  
  164.   
  165.         }  
  166.   
  167.         public boolean isWrapperFor(Class<?> arg0) throws SQLException {  
  168.             // TODO Auto-generated method stub  
  169.             return false;  
  170.         }  
  171.   
  172.         public <T> T unwrap(Class<T> arg0) throws SQLException {  
  173.             // TODO Auto-generated method stub  
  174.             return null;  
  175.         }  
  176.   
  177.         public Connection getConnection() throws SQLException {  
  178.             // TODO Auto-generated method stub  
  179.             return null;  
  180.         }  
  181.   
  182.         public Connection getConnection(String theUsername, String thePassword)  
  183.                 throws SQLException {  
  184.             // TODO Auto-generated method stub  
  185.             return null;  
  186.         }  
  187.     }  
  188.   
  189.     public String getMailhost() {  
  190.         return mailhost;  
  191.     }  
  192.   
  193.     public void setMailhost(String mailhost) {  
  194.         this.mailhost = mailhost;  
  195.         properties.setProperty("mail.host"this.mailhost);  
  196.     }  


实例下载地址

android崩溃日志保存SDCARD-本地阅读-并发送email

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警