使用fusedLocationProviderClient跟踪具有前台服务的用户位置时,Android应用程序崩溃

时间:2019-01-28 07:02:50

标签: android fusedlocationproviderapi foreground-service

我的Android应用程序有问题。她必须跟踪作为送货员的用户的位置。为此,我实现了前台服务和持久通知。我通过FusedLocationProviderClient获得职位。但是,一旦我启动该服务,该应用程序就会检索位置并在大约30秒后崩溃。在Android Studio日志中,我遇到以下错误

2019-01-28 07:31:42.971 9915-9915/? A/DEBUG: signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
2019-01-28 07:31:42.975 9915-9915/? A/DEBUG: Abort message: 'Invalid address 0x71d2e80210 passed to free: value not allocated'
2019-01-28 07:31:42.975 9915-9915/? A/DEBUG:     x0   0000000000000000  x1   0000000000002420  x2   0000000000000006  x3   0000000000000008
2019-01-28 07:31:42.975 9915-9915/? A/DEBUG:     x4   4473800044070000  x5   0000000000000000  x6   8080000080000000  x7   0000000000000008
2019-01-28 07:31:42.975 9915-9915/? A/DEBUG:     x8   0000000000000083  x9   ffffffffffffffdf  x10  0000000000000000  x11  0000000000000001
2019-01-28 07:31:42.975 9915-9915/? A/DEBUG:     x12  ffffffffffffffff  x13  0000000000000000  x14  0000000000000000  x15  0033cf84285c09f5
2019-01-28 07:31:42.975 9915-9915/? A/DEBUG:     x16  0000007216c90ee0  x17  0000007216c3a5ac  x18  00000000ffffffff  x19  00000071f70fa4f8
2019-01-28 07:31:42.975 9915-9915/? A/DEBUG:     x20  0000000000000006  x21  00000071f70fa450  x22  0000000000000000  x23  0000007216c9b7f8
2019-01-28 07:31:42.975 9915-9915/? A/DEBUG:     x24  0000007216c9b864  x25  00000000000fd000  x26  f25a83d93e14eed8  x27  00000072092afef8
2019-01-28 07:31:42.975 9915-9915/? A/DEBUG:     x28  7fffffffffffffff  x29  00000071f70fa050  x30  0000007216c37a54
2019-01-28 07:31:42.975 9915-9915/? A/DEBUG:     sp   00000071f70fa030  pc   0000007216c3a5b4  pstate 0000000060000000
2019-01-28 07:31:42.981 9915-9915/? A/DEBUG: backtrace:
2019-01-28 07:31:42.981 9915-9915/? A/DEBUG:     #00 pc 000000000006b5b4  /system/lib64/libc.so (tgkill+8)
2019-01-28 07:31:42.981 9915-9915/? A/DEBUG:     #01 pc 0000000000068a50  /system/lib64/libc.so (pthread_kill+64)
2019-01-28 07:31:42.981 9915-9915/? A/DEBUG:     #02 pc 0000000000023f68  /system/lib64/libc.so (raise+24)
2019-01-28 07:31:42.981 9915-9915/? A/DEBUG:     #03 pc 000000000001c9ec  /system/lib64/libc.so (abort+52)
2019-01-28 07:31:42.981 9915-9915/? A/DEBUG:     #04 pc 0000000000020f74  /system/lib64/libc.so (__libc_fatal+104)
2019-01-28 07:31:42.981 9915-9915/? A/DEBUG:     #05 pc 000000000008fe44  /system/lib64/libc.so (ifree+1304)
2019-01-28 07:31:42.981 9915-9915/? A/DEBUG:     #06 pc 000000000008fec8  /system/lib64/libc.so (je_free+128)
2019-01-28 07:31:42.981 9915-9915/? A/DEBUG:     #07 pc 000000000003bb5c  /system/lib64/libhwui.so (_ZN7android10uirenderer12renderthread12RenderThread10threadLoopEv+148)
2019-01-28 07:31:42.981 9915-9915/? A/DEBUG:     #08 pc 0000000000012418  /system/lib64/libutils.so (_ZN7android6Thread11_threadLoopEPv+272)
2019-01-28 07:31:42.981 9915-9915/? A/DEBUG:     #09 pc 00000000000a4d40  /system/lib64/libandroid_runtime.so (_ZN7android14AndroidRuntime15javaThreadShellEPv+116)
2019-01-28 07:31:42.981 9915-9915/? A/DEBUG:     #10 pc 0000000000068258  /system/lib64/libc.so (_ZL15__pthread_startPv+196)
2019-01-28 07:31:42.981 9915-9915/? A/DEBUG:     #11 pc 000000000001dc00  /system/lib64/libc.so (__start_thread+16)
2019-01-28 07:31:44.222 3070-3070/? E/audit: type=1400 audit(1548657104.212:2792): avc:  denied  { search } for  pid=9915 comm="debuggerd64" name="com.google.android.gms" dev="sda16" ino=1507533 scontext=u:r:debuggerd:s0 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=dir permissive=0 SEPF_SECMOBILE_7.0_0009
2019-01-28 07:31:44.298 3150-3150/? E/lowmemorykiller: Error writing /proc/8925/oom_score_adj; errno=22

我觉得系统已终止了该应用程序,但我不明白为什么。该应用程序即将完成,必须投入生产,但是在这里我阻止了。请帮助我

这是我的服务班级

public class GpsTrackingService extends Service {
public static String TAG = GpsTrackingService.class.getSimpleName();
public static Integer NOTIFICATION_ID = 1234595646;

public static String NOTIFICATION_CHANNEL_ID = "chanel_id";
public static String NOTIFICATION_CHANNEL_NAME = "chanel_name";
public static String NOTIFICATION_CHANNEL_DESCRIPTION = "chanel_desc";

public static Integer DEFAULT_REQUEST_LOCATION_INTERVAL = 10000;
public static Integer DEFAULT_MINIMUM_DISTANCE_TO_SAVE_LOCATION = 250;

private FusedLocationProviderClient locationProviderClient;
private LocationRequest locationRequest;
private LocationCallback locationCallback;

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

@Override
public void onCreate() {
    super.onCreate();
    locationProviderClient = LocationServices.getFusedLocationProviderClient(this);
    locationRequest = new LocationRequest();

    locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            final String path = getString(R.string.firebase_delivery_sessions_path);
            DatabaseReference ref = FirebaseDatabase.getInstance().getReference(path);
            Location location = locationResult.getLastLocation();

            DeliverySession deliverySession = Storage.getObject("deliverySession", DeliverySession.class);
            if (deliverySession != null && location != null) {
                WebServiceDataMap data = new WebServiceDataMap();
                data.id = deliverySession.id;
                data.latitude = location.getLatitude()+"";
                data.longitude = location.getLongitude()+"";
                ref.child(data.id+"").setValue(data);

                Location lastLocation = Storage.getObject("lastLocation", Location.class);
                if(lastLocation == null) {
                    WebService.getInstance().addDeliverySessionLocation(data);
                    Storage.setObject("lastLocation", location);
                }else {
                    int minimumDistanceToSaveLocation = DEFAULT_MINIMUM_DISTANCE_TO_SAVE_LOCATION;
                    try {
                        minimumDistanceToSaveLocation = AhaJogApplication.remoteSettings.geolocation.minimumDistanceToSaveLocation;
                    }catch(Exception e){}
                    float distance = location.distanceTo(lastLocation);
                    if(distance>= minimumDistanceToSaveLocation) {
                        WebService.getInstance().addDeliverySessionLocation(data);
                        Storage.setObject("lastLocation", location);
                    }
                }
            }
            Log.d("GpsTrackingService.java", "Location result");
        }
    };
}

private Notification getNotification() {

    Context context = getApplicationContext();

    PendingIntent action = PendingIntent.getActivity(context,
            0, new Intent(context, MainActivity.class),
            PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder;

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME,
                NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription(NOTIFICATION_CHANNEL_DESCRIPTION);
        manager.createNotificationChannel(channel);
        builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    }else {
        builder = new NotificationCompat.Builder(context);
    }

    return builder.setContentIntent(action)
            .setContentTitle("AHA JOG GPS Tracker")
            .setContentText("Tracking enabled")
            .setSmallIcon(R.drawable.aha_jog_logo)
            .setContentIntent(action)
            .setOngoing(true).build();
}

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

    if (intent != null && intent.getAction().contains("start")) {
        startForeground(NOTIFICATION_ID, getNotification());
        loginToFirebase();
    } else if(intent != null) {
        stopForeground(true);
        locationProviderClient.removeLocationUpdates(locationCallback);
        stopSelf();
    }
    return Service.START_STICKY;
}

private void loginToFirebase() {
    String email = getString(R.string.admin_email);
    String password = getString(R.string.admin_password);
    FirebaseAuth.getInstance().signInWithEmailAndPassword(
            email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(Task<AuthResult> task) {
            if (task.isSuccessful()) {
                requestLocationUpdates();
            } else {
                Log.d(TAG, "Firebase authentication failed : "+task.getException().getMessage());
            }
        }
    });
}

private void requestLocationUpdates() {

    long requestLocationInterval = DEFAULT_REQUEST_LOCATION_INTERVAL;
    int priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY;

    try {
        requestLocationInterval = AhaJogApplication.remoteSettings.geolocation.requestLocationInterval;
    }catch(Exception e){}

    try {
        if (RemoteSettings.LOCATION_HIGHT_PRECISION.equals(AhaJogApplication.remoteSettings.geolocation.precision)) {
            priority = LocationRequest.PRIORITY_HIGH_ACCURACY;
        }
        if (RemoteSettings.LOCATION_MEDIUM_PRECISION.equals(AhaJogApplication.remoteSettings.geolocation.precision)) {
            priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY;
        }
        if (RemoteSettings.LOCATION_LOW_PRECISION.equals(AhaJogApplication.remoteSettings.geolocation.precision)) {
            priority = LocationRequest.PRIORITY_LOW_POWER;
        }
    }catch(Exception e){}

    locationRequest.setPriority(priority);
    locationRequest.setInterval(requestLocationInterval);
    int permission = ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION);
    if (permission == PackageManager.PERMISSION_GRANTED) {
        locationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
    }
}

public static Promise requireLocationManager(final Context context, boolean exit) {
    final Promise promise = new Promise();
    LocationManager lm = (LocationManager) context.getSystemService(LOCATION_SERVICE);
    if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        if(exit) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    promise.reject(null);
                }
            }, 100);
            return promise;
        }
            DialogTools.showConfirm((Activity)context, "Location services are disabled", "You must enable them to continue", new Runnable() {
            @Override
            public void run() {
                context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
            }
        }, new Runnable() {
            @Override
            public void run() {
                DialogTools.showAlert((Activity) context, "You must enable location services to continue", "The operation can not be performed", null);
                promise.reject(null);
            }
        });
    }else {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                promise.resolve(true);
            }
        }, 100);
        return promise;
    }
    return requireLocationManager(context, true);
}

@Override
public void onDestroy() {
    super.onDestroy();
    locationProviderClient.removeLocationUpdates(locationCallback);
    Storage.remove("lastLocation");
}

}

这是清单文件中与服务相关的部分

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
    android:allowBackup="true"
    android:debuggable="true"
    android:hardwareAccelerated="true"
    android:theme="@style/AppTheme"
    tools:ignore="HardcodedDebugMode">
<service android:name=".services.GpsTrackingService" />
</application>

1 个答案:

答案 0 :(得分:0)

您可以尝试从那里创建新线程并启动服务吗?