首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android位置跟踪器在很长一段时间后崩溃

Android位置跟踪器在很长一段时间后崩溃
EN

Stack Overflow用户
提问于 2015-09-22 08:07:39
回答 1查看 304关注 0票数 1

我已经创建了一个位置跟踪应用程序,它在每次更改位置时都会写入本地SQLite数据库。该应用程序不幸崩溃后,大约7-8小时的跟踪,不幸的是,这没有发生时,我已经连接到一个调试器,所以没有日志,我可以附加。一些更多可能有用的信息:

  • 这个应用程序在从后台被唤醒之前就崩溃了(可以在跟踪的数据中清楚地看到这一点),所以我可以从其他应用程序中排除这个bug。
  • 试图写入文本文件而不是数据库,但没有成功(只是在崩溃前3小时运行)
  • 更改跟踪间隔(5s正常的1s最快间隔):相同的结果应用程序在7-8小时后也会崩溃。

下面是一些代码片段:

位置变化事件

代码语言:javascript
复制
@Override
public void onLocationChanged(Location location) {
    if(location == null){
        location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if(location == null) {
            return;
        }
    }
    Log.d(TAG, location.toString());
    double currentLatitude = location.getLatitude();
    double currentLongitude = location.getLongitude();
    ActivitylogRepo activityRepo = new ActivitylogRepo(this);
    Activitylog activity = new Activitylog();
    activity.activity = "Position";
    activity.timestamp = getDateTime();
    activity.value2 = String.valueOf(currentLatitude);
    activity.value3 = String.valueOf(currentLongitude);
    activityRepo.insert(activity);
}

数据库插入命令

代码语言:javascript
复制
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;
}

初始化服务

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-22 08:20:18

我有一些东西可以帮助您捕捉您的崩溃报告,即使您没有连接到调试器--从而帮助您找到问题的根源!我已经张贴了这个作为答案,所以我可以很好地格式化它。

下面的类将处理不明确的异常,将它们打包到电子邮件中,并将通知放在您的电话上(您可以根据您的需要对此进行调整)。

代码语言:javascript
复制
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);
    }
}

在应用程序类中设置如下所示:

代码语言:javascript
复制
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));

    }

}

我不建议将此代码包含在您的发布版本中!此时,您可以使用开发人员控制台获取崩溃报告。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32711640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档