ForegroundService 由于内存不足而被杀死

问题描述 投票:0回答:1

我不知道我还能做些什么来防止我的应用程序被 Android 杀死。 我找不到积累记忆的地方。

在前台服务中,我通过 requestLocationUpdates 接收我的纬度/经度。 然后,我通过 BroadcastReceiver 将这两个值传递到我的 MainActivity,在其中我将这些值添加到列表 polylinePoints 中,然后绘制折线。

其他实时跟踪您的路径的应用程序永远不会被杀死,我不明白为什么。

也许有人可以帮助我?

我的应用程序也在 Github 上:https://github.com/bernd-roth/Ultrarunning

这是我的代码的一部分

private void createPolypoints(double lastLat, double lastLng, List<LatLng> polylinePoints) {
        if(isDistanceCovered) {
            boolean isServiceRunning = isServiceRunning(getString(R.string.serviceName));
            //isServiceRunning = true; // FIXME

        if (!isServiceRunning) {
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lastLat, lastLng), 0));
            toolbar_title.setText("Distance: 0.0 Km" + "\nSpeed: 0.0");
        } else {
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lastLat, lastLng), 16));
            //Projection projection = mMap.getProjection();

            if (startingPoint) {
                mMap.addMarker(new MarkerOptions().position(new LatLng(lastLat, lastLng)).title(getResources().getString(R.string.current_location))).showInfoWindow();
                startingPoint = false;
            } else {
                //polylinePoints = groupPoints(polylinePoints, projection);

                if(polyline!=null) {
                    polyline.remove();
                    mMap.clear();
                }

                polyline = mMap.addPolyline(new PolylineOptions().addAll(polylinePoints).color(Color.MAGENTA).jointType(JointType.ROUND).width(15.0f));
                toolbar_title.setText("Distance: " + String.format("%.2f", coveredDistance) + "\nSpeed: " + speed);
            }
        }
    }
}

private class DataBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (isDistanceCovered) {
                String action = intent.getAction();
                Timber.d("DataBroadcastReceiver %s", action);
//                ArrayList<Parcelable> polylinePointsParcelable = intent.getExtras().getParcelableArrayList(SharedPref.STATIC_BROADCAST_ACTION);
                oldLatitude = intent.getExtras().getDouble("OLD-LAT");
                oldLongitude = intent.getExtras().getDouble("OLD-LON");
                currentLatitude = intent.getExtras().getDouble("CURRENT-LAT");
                currentLongitude = intent.getExtras().getDouble("CURRENT-LON");

                if(currentLatitude != 0 && currentLongitude != 0) {
                    speed = intent.getExtras().getString("SPEED");
                    coveredDistance = intent.getExtras().getFloat("DISTANCE");

                    oldLatitude = currentLatitude;
                    oldLongitude = currentLongitude;

                    polylinePoints.add(new LatLng(oldLatitude, oldLongitude));

                    createPolypoints(oldLatitude, oldLongitude, polylinePoints);
                }

提前非常感谢您!

android google-polyline
1个回答
0
投票

您需要通过进行堆转储并检查哪些对象正在高速增长来进行调试。但是您可以采取一些措施来改进代码:

1)您的前台服务不应将数据传递给活动并将其直接添加到地图中。如果您的 Activity 由于不在前台而被删除怎么办?这可能会导致整个 Activity 泄漏。相反,将其发布到 RxJava 主题或 Flow 中的 ViewModel 之类的内容,并让 Activity 订阅它(并确保它取消订阅

2) 您多久获得一次位置信息?您实际上不想向地图添加大量更新。首先是因为有很多物体,其次是因为位置很吵,这会让你的地图看起来到处都是。您应该仅每隔几秒更新一次,并且仅当位置变化足够大时才更新。通过这种方式减少点数也会减少内存积累。

3)考虑通过删除旧积分来限制总积分。 30分钟前的数据真的需要吗?或者可以减少节省的积分吗?您稍后是否真的需要这种级别的准确性 - 您需要每分钟 60 次更新,还是稍后回顾数据时 1 次就足够了?这取决于您正在做什么,但几分钟后您可能会删除 90% 的数据点。

© www.soinside.com 2019 - 2024. All rights reserved.