当部署在Google Play商店中的应用崩溃时,您会看到这些崩溃报告。假设我有使用Log.e
捕获和记录的异常,有没有办法以任何方式查看这些异常?换句话说:当我想查看对我的工作流程而言真正重要的错误时,是否只能在Google Play开发者控制台中查看这些例外情况,以便在应用内投放RuntimeException
以使用户意图崩溃? (那真是太愚蠢了)。
背景:我目前正在实施应用内结算。实际上有很多阶段可能会在购买过程中发生可能的异常。如果客户与我联系并询问购买流程无效的原因,我应该如何知道Google Play Developer Console中是否有任何信息?
你可以使用Firebase Crashlytics
Firebase Crashlytics是一款轻量级的实时崩溃记录器,可帮助您跟踪,确定优先级并解决影响应用质量的稳定性问题。 Crashlytics通过智能地对崩溃进行分组并突出显示导致崩溃的情况,从而为您节省故障排除时间。
关键能力
你可以使用Fire base crash report service.i附上了codelabs示例代码(官方文档)。 sample code
你可以在技术上使用Crashlytics或HockeyApp,但它可能无法帮助你,因为它只会在应用程序崩溃时触发。我的意思是这很好,但问题是如果用户有问题并且应用程序没有崩溃(购买过程没有完成)。您可以通过以下方式处理此问题(我在我的应用程序中实现了此方法):
码:
public static void sendLog(Context context) {
//set a file
Date datum = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
String fullName = df.format(datum) + "appLog.txt";
File file = new File(Environment.getExternalStorageDirectory(), fullName);
//clears a previous log
if (file.exists()) {
file.delete();
}
//write log to file
int pid = android.os.Process.myPid();
try {
String command = String.format("logcat -d -v threadtime *:*");
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder result = new StringBuilder();
String currentLine = null;
while ((currentLine = reader.readLine()) != null) {
if (currentLine != null && currentLine.contains(String.valueOf(pid))) {
result.append(currentLine);
result.append("\n");
}
}
FileWriter out = new FileWriter(file);
out.write(result.toString());
out.close();
sendEmail(context, file);
} catch (IOException e) {
e.printStackTrace();
}
//clear the log
try {
Runtime.getRuntime().exec("logcat -c");
} catch (IOException e) {
e.printStackTrace();
}
}
发送文件到电子邮件:
private static void sendEmail(Context context, File file) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"XXXXXXXXXX ENTER EMAIL"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Log Report");
intent.putExtra(Intent.EXTRA_TEXT, "Add description:");
if (!file.exists() || !file.canRead()) {
Toast.makeText(context, "Attachment Error", Toast.LENGTH_SHORT).show();
return;
}
Uri uri = Uri.parse("file://" + file);
intent.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(Intent.createChooser(intent, "Send email..."));
}