在Android Oreo上关闭屏幕和Wi-Fi时更新位置

时间:2019-03-18 11:15:07

标签: android background geolocation wifi android-8.0-oreo

我正在编写应用程序以跟踪用户的位置。我在装有Android Oreo的平板电脑Huawei MediaPad T5上启动了我的应用程序。 我在Android 8上了解了后台限制中的位置更新。因此,我使用前台服务。打开Wi-Fi时,我的应用程序运行良好。如果我将Wi-Fi切换为关闭,则屏幕关闭时,应用程序无法获取位置信息。 我将扫描Wi-Fi设置为打开。

我在MainActivity中启动前台服务

Intent trackerService = new Intent(this, TrackerService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    startForegroundService(trackerService);
}

在TrackerService中,我具有onCreate方法,该方法创建并显示前台通知。

@Override
public void onCreate() {
    super.onCreate();

    Context context = getAplicationContext();
    String contentText = "App works with location";
    Notification notification;
    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    NotificationManager notificationManager;
    String CHANNEL_ID = "my_channel_01";
    CharSequence name = context.getString(R.string.channel_name);
    int importance = NotificationManager.IMPORTANCE_HIGH;
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
    notification = new NotificationCompat.Builder(context, CHANNEL_ID)
                    .setContentTitle("Base")
                    .setContentText(contentText)
                    .setSmallIcon(R.drawable.icon)
                    .setContentIntent(pendingIntent)
                    .setOngoing(true)
                    .build();
    notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
    if (notificationManager != null) {
         notificationManager.createNotificationChannel(channel);
         notificationManager.notify(1, notification);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForeground(1, notification);
    } 
}

在onStartCommand中,我初始化位置请求,回调和请求更新。

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    locationRequest = LocationRequest.create();
    locationRequest.setInterval(1000 * 50);
    locationRequest.setFastestInterval(1000 * 30);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    LocationCallback locationCallback = new LocationCallback() {

        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                return;
            }
            for (Location location : locationResult.getLocations()) {
                Log.i("LOCATION_TAG", location.getProvider() + " " + location.getAccuracy() + " " + getFormattedDate(location.getTime()));
            }
        }
    };

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return -1;
        }
    }
    FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);

    return START_REDELIVER_INTENT;
}

我也尝试在onCreate中使用WakeLock,但是它不能解决我的问题。

PowerManager mgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "LOCATION + ":" + "WAKE LOCK");
wakeLock.acquire();

如何解决此问题?

0 个答案:

没有答案