2015-01-05 29 views

回答

0

我更喜歡使用像Crashlytics這樣的框架。我用同樣的它可以幫助您跟蹤下來的崩潰和異常,以及,而不是實現自己的:)

+0

感謝Virendrao。 但我需要在我的應用程序沒有第三方應用程序。 –

+0

1.您需要互聯網許可。 2.當你發現異常。複製堆棧跟蹤並將其粘貼到電子郵件意圖的主題中。 3.將永遠是您的電子郵件地址 您可以按照此步驟操作。 :)乾杯 – virendrao

2

package tv.demogtelog; 
 

 
import java.io.BufferedReader; 
 
import java.io.File; 
 
import java.io.FileOutputStream; 
 
import java.io.FileReader; 
 
import java.io.FilenameFilter; 
 
import java.io.IOException; 
 
import java.io.PrintWriter; 
 
import java.io.StringWriter; 
 
import java.io.Writer; 
 
import java.util.Date; 
 
import java.util.Random; 
 

 
import android.content.Context; 
 
import android.content.Intent; 
 
import android.content.pm.PackageInfo; 
 
import android.content.pm.PackageManager; 
 
import android.content.pm.PackageManager.NameNotFoundException; 
 
import android.os.Environment; 
 
import android.os.StatFs; 
 

 
import com.example.demogetlog.R; 
 

 
public class ErrorReporter implements Thread.UncaughtExceptionHandler { 
 
\t 
 
\t String VersionName; 
 
\t String PackageName; 
 
\t String FilePath; 
 
\t String PhoneModel; 
 
\t String AndroidVersion; 
 
\t String Board; 
 
\t String Brand; 
 
\t // String CPU_ABI; 
 
\t String Device; 
 
\t String Display; 
 
\t String FingerPrint; 
 
\t String Host; 
 
\t String ID; 
 
\t String Manufacturer; 
 
\t String Model; 
 
\t String Product; 
 
\t String Tags; 
 
\t long Time; 
 
\t String Type; 
 
\t String User; 
 

 
\t private Thread.UncaughtExceptionHandler PreviousHandler; 
 
\t private static ErrorReporter S_mInstance; 
 
\t private Context  CurContext; 
 
\t 
 
\t public void Init(Context context) 
 
\t { 
 
\t PreviousHandler = Thread.getDefaultUncaughtExceptionHandler(); 
 
\t Thread.setDefaultUncaughtExceptionHandler(this); 
 
\t RecoltInformations(context); 
 
\t CurContext = context; 
 
\t } 
 
\t 
 
\t public long getAvailableInternalMemorySize() { 
 
\t   File path = Environment.getDataDirectory(); 
 
\t   StatFs stat = new StatFs(path.getPath()); 
 
\t   long blockSize = stat.getBlockSize(); 
 
\t   long availableBlocks = stat.getAvailableBlocks(); 
 
\t   return availableBlocks * blockSize; 
 
\t  } 
 
\t  
 
\t public long getTotalInternalMemorySize() { 
 
\t   File path = Environment.getDataDirectory(); 
 
\t   StatFs stat = new StatFs(path.getPath()); 
 
\t   long blockSize = stat.getBlockSize(); 
 
\t   long totalBlocks = stat.getBlockCount(); 
 
\t   return totalBlocks * blockSize; 
 
\t  } 
 
\t void RecoltInformations(Context context) 
 
\t { 
 
\t PackageManager pm = context.getPackageManager(); 
 
\t   try 
 
\t   { 
 
\t   PackageInfo pi; 
 
\t    // Version 
 
\t    pi = pm.getPackageInfo(context.getPackageName(), 0); 
 
\t    VersionName = pi.versionName; 
 
\t    // Package name 
 
\t    PackageName = pi.packageName; 
 
\t    // Files dir for storing the stack traces 
 
\t    FilePath = context.getFilesDir().getAbsolutePath(); 
 
\t    // Device model 
 
\t    PhoneModel = android.os.Build.MODEL; 
 
\t    // Android version 
 
\t    AndroidVersion = android.os.Build.VERSION.RELEASE; 
 
\t    
 
\t    Board = android.os.Build.BOARD; 
 
\t    Brand = android.os.Build.BRAND; 
 
\t    //CPU_ABI = android.os.Build.; 
 
\t    Device = android.os.Build.DEVICE; 
 
\t    Display = android.os.Build.DISPLAY; 
 
\t    FingerPrint = android.os.Build.FINGERPRINT; 
 
\t   Host = android.os.Build.HOST; 
 
\t   ID = android.os.Build.ID; 
 
\t   //Manufacturer = android.os.Build.; 
 
\t   Model = android.os.Build.MODEL; 
 
\t   Product = android.os.Build.PRODUCT; 
 
\t   Tags = android.os.Build.TAGS; 
 
\t   Time = android.os.Build.TIME; 
 
\t   Type = android.os.Build.TYPE; 
 
\t   User = android.os.Build.USER; 
 
\t    
 
\t   } 
 
\t   catch (NameNotFoundException e) 
 
\t   { 
 
\t     e.printStackTrace(); 
 
\t   } 
 
\t } 
 
\t 
 
\t public String CreateInformationString() 
 
\t { 
 
\t String ReturnVal = ""; 
 
\t ReturnVal += "Version : " + VersionName; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "Package : " + PackageName; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "FilePath : " + FilePath; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "Phone Model" + PhoneModel; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "Android Version : " + AndroidVersion; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "Board : " + Board; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "Brand : " + Brand; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "Device : " + Device; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "Display : " + Display; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "Finger Print : " + FingerPrint; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "Host : " + Host; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "ID : " + ID; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "Model : " + Model; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "Product : " + Product; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "Tags : " + Tags; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "Time : " + Time; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "Type : " + Type; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "User : " + User; 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "Total Internal memory : " + getTotalInternalMemorySize(); 
 
\t ReturnVal += "\n"; 
 
\t ReturnVal += "Available Internal memory : " + getAvailableInternalMemorySize(); 
 
\t ReturnVal += "\n"; 
 
\t 
 
\t return ReturnVal; 
 
\t } 
 
\t 
 

 
\t @Override 
 
\t public void uncaughtException(Thread thread, Throwable e) { 
 
\t \t String Report = ""; 
 
\t \t Date CurDate = new Date(); 
 
\t \t Report += "Error Report collected on : " + CurDate.toString(); 
 
\t \t Report += "\n"; 
 
\t \t Report += "\n"; 
 
\t \t Report += "Informations :"; 
 
\t \t Report += "\n"; 
 
\t \t Report += "=============="; 
 
\t \t Report += "\n"; 
 
\t \t Report += "\n"; 
 
\t \t Report += CreateInformationString(); 
 
\t \t  
 
\t \t Report += "\n\n"; 
 
\t \t Report += "Stack : \n"; 
 
\t \t Report += "======= \n"; 
 
\t \t final Writer result = new StringWriter(); 
 
\t \t final PrintWriter printWriter = new PrintWriter(result); 
 
\t \t e.printStackTrace(printWriter); 
 
\t \t String stacktrace = result.toString(); 
 
\t \t Report += stacktrace; 
 

 
\t \t Report += "\n"; 
 
\t \t Report += "Cause : \n"; 
 
\t \t Report += "======= \n"; 
 
\t \t 
 
\t \t // If the exception was thrown in a background thread inside 
 
\t \t // AsyncTask, then the actual exception can be found with getCause 
 
\t \t Throwable cause = e.getCause(); 
 
\t \t while (cause != null) 
 
\t \t { 
 
\t \t cause.printStackTrace(printWriter); 
 
\t \t Report += result.toString(); 
 
\t \t cause = cause.getCause(); 
 
\t \t } 
 
\t \t printWriter.close(); 
 
\t \t Report += "**** End of current Report ***"; 
 
\t \t SaveAsFile(Report); 
 
\t \t //SendErrorMail(Report); 
 
\t \t CheckErrorAndSendMail(CurContext); 
 
\t \t PreviousHandler.uncaughtException(thread, e); 
 
\t \t } 
 
\t \t 
 
\t \t static ErrorReporter getInstance() 
 
\t \t { 
 
\t \t if (S_mInstance == null) 
 
\t \t S_mInstance = new ErrorReporter(); 
 
\t \t return S_mInstance; 
 
\t \t 
 
\t } 
 
\t \t 
 
\t \t private void SendErrorMail(Context _context, String ErrorContent) 
 
\t \t { 
 
\t \t Intent sendIntent = new Intent(Intent.ACTION_SEND); 
 
\t \t String subject = _context.getResources().getString(R.string.CrashReport_MailSubject); 
 
\t \t String body = _context.getResources().getString(R.string.CrashReport_MailBody) + 
 
\t \t "\n\n"+ 
 
\t \t ErrorContent+ 
 
\t \t "\n\n"; 
 
\t \t sendIntent.putExtra(Intent.EXTRA_EMAIL, 
 
\t \t  new String[] {"[email protected]"}); 
 
\t \t sendIntent.putExtra(Intent.EXTRA_TEXT, body); 
 
\t \t sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject); 
 
\t \t sendIntent.setType("message/rfc822"); 
 
\t \t _context.startActivity(Intent.createChooser(sendIntent, "Title:")); 
 
\t \t } 
 
\t \t private void SaveAsFile(String ErrorContent) 
 
\t \t { 
 
\t \t try 
 
\t \t { 
 
\t \t Random generator = new Random(); 
 
\t \t int random = generator.nextInt(99999); 
 
\t \t String FileName = "stack-" + random + ".stacktrace"; 
 
\t \t FileOutputStream trace = CurContext.openFileOutput(FileName, Context.MODE_PRIVATE); 
 
\t \t trace.write(ErrorContent.getBytes()); 
 
\t \t trace.close(); 
 
\t \t } 
 
\t \t catch(IOException ioe) { 
 
\t \t // ... 
 
\t \t } 
 
\t \t } 
 
\t \t 
 
\t \t private String[] GetErrorFileList() 
 
\t \t { 
 
\t \t File dir = new File(FilePath + "/"); 
 
\t \t   // Try to create the files folder if it doesn't exist 
 
\t \t   dir.mkdir(); 
 
\t \t   // Filter for ".stacktrace" files 
 
\t \t   FilenameFilter filter = new FilenameFilter() { 
 
\t \t     public boolean accept(File dir, String name) { 
 
\t \t       return name.endsWith(".stacktrace"); 
 
\t \t     } 
 
\t \t   }; 
 
\t \t   return dir.list(filter); 
 
\t \t } 
 
\t \t private boolean bIsThereAnyErrorFile() 
 
\t \t { 
 
\t \t return GetErrorFileList().length > 0; 
 
\t \t } 
 
\t \t public void CheckErrorAndSendMail(Context _context) 
 
\t \t { 
 
\t \t try 
 
\t \t { 
 
\t \t if (bIsThereAnyErrorFile()) 
 
\t \t { 
 
\t \t  String WholeErrorText = ""; 
 
\t \t  String[] ErrorFileList = GetErrorFileList(); 
 
\t \t  int curIndex = 0; 
 
\t \t  // We limit the number of crash reports to send (in order not to be too slow) 
 
\t \t  final int MaxSendMail = 5; 
 
\t \t  for (String curString : ErrorFileList) 
 
\t \t  { 
 
\t \t  if (curIndex++ <= MaxSendMail) 
 
\t \t  { 
 
\t \t  WholeErrorText+="New Trace collected :\n"; 
 
\t \t  WholeErrorText+="=====================\n "; 
 
\t \t  String filePath = FilePath + "/" + curString; 
 
\t \t  BufferedReader input = new BufferedReader(new FileReader(filePath)); 
 
\t \t  String line; 
 
\t \t  while ((line = input.readLine()) != null) 
 
\t \t  { 
 
\t \t  WholeErrorText += line + "\n"; 
 
\t \t  } 
 
\t \t  input.close(); 
 
\t \t  } 
 

 
\t \t  // DELETE FILES !!!! 
 
\t \t  File curFile = new File(FilePath + "/" + curString); 
 
\t \t  curFile.delete(); 
 
\t \t  } 
 
\t \t  SendErrorMail(_context , WholeErrorText); 
 
\t \t } 
 
\t \t } 
 
\t \t catch(Exception e) 
 
\t \t { 
 
\t \t e.printStackTrace(); 
 
\t \t } 
 
\t \t } 
 

 
}

相關問題