嘿伙计我需要帮助配置一些源代码,基本上当发生错误时通过电子邮件向我发送崩溃报告。这里有一些我正在玩的代码我只需要有一点java经验的人忽略它并帮我清理它。
谢谢,
public final class EmailUtils {
private EmailUtils()
{
}
private static int bytesToMb(long l)
{
return (int)((double)l / 1024D / 1024D);
}
private static int getAvailableInternalMemorySize()
{
StatFs statfs = new StatFs(Environment.getDataDirectory().getPath());
return bytesToMb((long)statfs.getBlockSize() * (long)statfs.getAvailableBlocks());
}
private static String getEmailText(Context context)
{
String s = "";
String s11;
PackageInfo packageinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
String s1 = packageinfo.versionName;
int i = packageinfo.versionCode;
String s2 = packageinfo.packageName;
String s3 = Build.MODEL;
String s4 = android.os.Build.VERSION.RELEASE;
String s5 = android.os.Build.VERSION.SDK;
String s6 = Build.DEVICE;
String s7 = Build.DISPLAY;
String s8 = Build.ID;
String s9 = (new StringBuilder(String.valueOf(Build.BRAND))).append("/").append(Build.MANUFACTURER).toString();
String s10 = Build.PRODUCT;
s = (new StringBuilder(String.valueOf(s))).append("\n\n------------------\n").toString();
s = (new StringBuilder(String.valueOf(s))).append("Version: ").append(s1).toString();
s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
s = (new StringBuilder(String.valueOf(s))).append("Version num: ").append(i).toString();
s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
s = (new StringBuilder(String.valueOf(s))).append("Package: ").append(s2).toString();
s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
s = (new StringBuilder(String.valueOf(s))).append("Phone Model: ").append(s3).toString();
s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
s = (new StringBuilder(String.valueOf(s))).append("Android Version: ").append(s4).toString();
s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
s = (new StringBuilder(String.valueOf(s))).append("SDK Version: ").append(s5).toString();
s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
s = (new StringBuilder(String.valueOf(s))).append("Device: ").append(s6).toString();
s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
s = (new StringBuilder(String.valueOf(s))).append("Product: ").append(s10).toString();
s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
s = (new StringBuilder(String.valueOf(s))).append("Brand: ").append(s9).toString();
s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
s = (new StringBuilder(String.valueOf(s))).append("Disp: ").append(s7).toString();
s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
s = (new StringBuilder(String.valueOf(s))).append("Id: ").append(s8).toString();
s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
s = (new StringBuilder(String.valueOf(s))).append("Available Internal memory: ").append(getAvailableInternalMemorySize()).append("/").append(getTotalInternalMemorySize()).append(" Mb").toString();
s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
s = (new StringBuilder(String.valueOf(s))).append("Heap size: ").append(getHeapSizeMb()).append(" Mb").toString();
s = (new StringBuilder(String.valueOf(s))).append("\n").toString();
s = (new StringBuilder(String.valueOf(s))).append("Mar: GO").toString();
s11 = (new StringBuilder(String.valueOf(s))).append("\n").toString();
s = s11;
android.content.pm.PackageManager.NameNotFoundException namenotfoundexception;
// namenotfoundexception.printStackTrace();
// if(true)
// goto
}
private static int getHeapSizeMb()
{
return bytesToMb(Runtime.getRuntime().maxMemory());
}
private static int getTotalInternalMemorySize()
{
StatFs statfs = new StatFs(Environment.getDataDirectory().getPath());
return bytesToMb((long)statfs.getBlockSize() * (long)statfs.getBlockCount());
}
public static void sendContactEmail(Context context)
{
String s = context.getResources().getString(0x7f040000);
String s1 = "";
String s2;
String s3;
Intent intent;
try
{
s1 = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
}
catch(android.content.pm.PackageManager.NameNotFoundException namenotfoundexception) { }
s2 = (new StringBuilder("[SUPPORT] ")).append(s).append(" ").append(s1).toString();
s3 = getEmailText(context);
intent = new Intent("android.intent.action.SEND");
intent.setType("plain/text");
intent.putExtra("android.intent.extra.EMAIL", new String[] {
"support@myemail.com"
});
intent.putExtra("android.intent.extra.SUBJECT", s2);
intent.putExtra("android.intent.extra.TEXT", s3);
if(context.getPackageManager().queryIntentActivities(intent, 0).size() > 0)
{
context.startActivity(Intent.createChooser(intent, "Send mail..."));
} else
{
CustomAlertDialogBuilder customalertdialogbuilder = new CustomAlertDialogBuilder(context);
customalertdialogbuilder.setTitle("Contact us");
customalertdialogbuilder.setIcon(R.drawable.ic_launcher);
customalertdialogbuilder.setMessage(Html.fromHtml("No application found to send emails.<br /><br />Please contact us at: <b>support@email.com</b>."));
customalertdialogbuilder.setNegativeButton("Close", null);
customalertdialogbuilder.show();
}
}
}
答案 0 :(得分:6)
我的主要目标是提取系统数据和意图。您是否建议使用ACRA方式或使用我的字符串方法?
绝对是ACRA。同样,它已经存在,受支持,提供多种方式向您提供数据(包括不泄露您的电子邮件地址的方式),与BugSense等第三方服务集成。
更具体地说明我的两个代码注释:
绝不会永远不会永远不会永远不会硬编码资源ID十六进制值,例如:String s = context.getResources().getString(0x7f040000);
。这些值可能随每次构建而变化,尤其是在添加/删除资源时。始终使用资源ID符号(例如,R.string.foo
)。
在getEmailText()
中,请创建一个 StringBuilder
对象并附加到其中。不要创建30个左右,每次使用一次并丢弃它们。