为什么当我启动服务时,应用程序开始等待GC分配阻塞时会崩溃?

我正在启动应用启动之初启动Service(位置服务)。但是在使用该应用程序(大约5至10分钟)后,我连续收到 Waiting for a blocking GC Alloc 错误,我的应用程序卡住了一段时间,然后突然崩溃了。 另外,当我在没有Service的情况下运行该应用程序时,该应用程序会正常运行。

我已经搜索了很多天,但没有任何解决方法。我检查了许多有关堆栈溢出的相关问题,但似乎没有任何适当或有价值的答案。

我也尝试在清单和android:largeHeap="true"中添加multiDexEnabled true,但这些甚至都没有帮助。

这是我的服务班

public class LocationService extends IntentService {

    private Context context;
    private FusedLocationProviderClient fusedLocationClient;
    private LocationRequest locationRequest;
    Task<LocationSettingsResponse> task;

    public static String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp",CHANNEL_NAME = "My Background Service";

    public LocationService() {
        super("LocationService");
    }
@Override
    public void onCreate() {
        super.onCreate();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            startmyownForeground();
        else startForeground(8,new Notification());

        createLocationRequest();


        context = this;
        fusedLocationClient = LocationServices.getFusedLocationProviderClient(context);


        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
        SettingsClient client = LocationServices.getSettingsClient(context);
        task = client.checkLocationSettings(builder.build());

    }


    @Override
    public int onStartCommand(@Nullable Intent intent,int flags,int startId) {

        try {
            fusedLocationClient.requestLocationUpdates(locationRequest,locationCallback,Looper.getMainLooper());
        } catch (SecurityException ignore) {
            Log.e("AppLocationService","SecurityException - " + ignore.toString(),ignore);
        }


        return Service.START_STICKY;
    }


    private final LocationCallback locationCallback = new LocationCallback() {

        @Override
        public void onLocationResult(LocationResult locationResult) {
            List<Location> locationList = locationResult.getLocations();
            if (locationList.size() != 0) {

                Location location = locationList.get(0);

                double latitude = location.getLatitude();
                double longitude = location.getLongitude();

                Intent intent = new Intent("location_update");
                intent.putExtra("latitude",lat);
                intent.putExtra("longitude",lng);
                sendBroadcast(intent);
            }
        }
    };

 @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {


    }


    protected void createLocationRequest() {
        locationRequest = LocationRequest.create();
        locationRequest.setInterval(2000);
        locationRequest.setfastestInterval(2000);
        locationRequest.setPriority(CommonObjects.LOCATION_PRIORITY);
    }
}

我的Logcat

为什么当我启动服务时,应用程序开始等待GC分配阻塞时会崩溃?

meihaoyong 回答:为什么当我启动服务时,应用程序开始等待GC分配阻塞时会崩溃?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3121775.html

大家都在问