Android位置跟踪器在很长一段时间后崩溃
我创建了一个位置跟踪应用程序,每当位置发生变化时,它都会写入本地SQLite数据库。 不幸的是,该应用程序在跟踪过程中大约7-8小时后崩溃,不幸的是,当我将设备连接到调试器时,不会发生这种情况,因此没有可以附加的日志。 更多可能有用的信息:Android位置跟踪器在很长一段时间后崩溃,android,sqlite,tracker,location-services,Android,Sqlite,Tracker,Location Services,我创建了一个位置跟踪应用程序,每当位置发生变化时,它都会写入本地SQLite数据库。 不幸的是,该应用程序在跟踪过程中大约7-8小时后崩溃,不幸的是,当我将设备连接到调试器时,不会发生这种情况,因此没有可以附加的日志。 更多可能有用的信息: 应用程序在从后台唤醒之前崩溃(可以在跟踪的数据中清楚地看到),因此我可以从其他应用程序中排除此错误 尝试写入文本文件而不是数据库,但未成功(在崩溃前仅运行了约3小时) 更改跟踪间隔(5s正常1s最快间隔):同样的结果应用程序在7-8小时后也会崩溃 以下是
- 应用程序在从后台唤醒之前崩溃(可以在跟踪的数据中清楚地看到),因此我可以从其他应用程序中排除此错误
- 尝试写入文本文件而不是数据库,但未成功(在崩溃前仅运行了约3小时)
- 更改跟踪间隔(5s正常1s最快间隔):同样的结果应用程序在7-8小时后也会崩溃
我有一些东西可以帮助您捕获崩溃报告,即使您没有连接到调试器——从而帮助您找到问题的根源!我已经把它作为一个答案发布了,所以我可以很好地格式化它 下面的类将处理未捕获的异常,将它们打包到电子邮件中,并在手机上发送通知(您可以根据需要进行调整) 在应用程序类中设置如下:
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
/*
* Set up uncaught exception handler
*/
java.lang.Thread.UncaughtExceptionHandler defaultUEH =
Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new
UncaughtExceptionHandler(this, defaultUEH));
}
}
我不建议在发布版本中包含此代码!此时,您可以使用开发人员控制台获取崩溃报告。您可以发布崩溃日志吗?正如我前面提到的,使用调试器时错误从未出现,我忘了补充一点,连接到Android Studio时也不会发生错误。谢谢您,到目前为止,我将尝试一下,不幸的是,上面提到的大约需要8个小时才能得到新的结果。我知道这很令人沮丧,但如果没有坠机报告,你就瞎了眼!我们明天会在这里;-)很抱歉,我没有更新任何信息,但我真的想在再次发布之前测试所有内容。你的解决方案确实帮助我找到了解决问题的办法。这只是一个简单的NullPointerException,它被抛出到我在后台线程中创建的JSONObject上。目前,该应用程序几乎24小时运行,没有任何问题。非常感谢您的解决方案,它绝对有帮助,也将有助于我未来的发展。
public int insert(Activitylog activitylog) {
//Open connection to write data
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Activitylog.KEY_activity, activitylog.activity);
values.put(Activitylog.KEY_timestamp, activitylog.timestamp);
values.put(Activitylog.KEY_value1, activitylog.value1);
values.put(Activitylog.KEY_value2, activitylog.value2);
values.put(Activitylog.KEY_value3, activitylog.value3);
values.put(Activitylog.KEY_value4, activitylog.value4);
// Inserting Row
long activitylog_id = db.insert(Activitylog.TABLE, null, values);
db.close(); // Closing database connection
return (int) activitylog_id;
}
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(20 * 1000) // 20s in ms
.setFastestInterval(5 * 1000); // 5s in ms
public class UncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
private Context mContext;
private java.lang.Thread.UncaughtExceptionHandler mDefaultUEH;
public UncaughtExceptionHandler(Context context, java.lang.Thread.UncaughtExceptionHandler defaultUEH) {
mContext = context;
mDefaultUEH = defaultUEH;
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// Make error into something more readable
String timestamp = android.text.format.DateFormat.getLongDateFormat(mContext).format(
new Date(System.currentTimeMillis()));
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
ex.printStackTrace(printWriter);
String stacktrace = result.toString();
printWriter.close();
// Create email intent for error
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(Intent.EXTRA_EMAIL, "email address");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Crash Report " + timestamp);
emailIntent.putExtra(Intent.EXTRA_TEXT, stacktrace);
// Make into pending intent for notifcation
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
Intent.createChooser(emailIntent, "Send report with.."),
Intent.FLAG_ACTIVITY_NEW_TASK);
// here create a notification for the user
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
builder.setContentTitle("Crash Caught");
builder.setContentText("Send to Developer");
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.ic_notification);
// Finally display the notification!
NotificationManager mNotificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1337, builder.build());
// re-throw critical exception further to the os (important)
mDefaultUEH.uncaughtException(thread, ex);
}
}
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
/*
* Set up uncaught exception handler
*/
java.lang.Thread.UncaughtExceptionHandler defaultUEH =
Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new
UncaughtExceptionHandler(this, defaultUEH));
}
}