java - 服务中的 Android 位置管理器

标签 java android service location

我曾尝试创建一个 IntentService,但后来我在 Internet 上了解到我应该改用服务。我实际上并不知道我在代码上做错了什么。有人可以给我解释一下吗?

我正在尝试创建一个简单的服务来读取纬度和经度,即使应用程序从屏幕上滑开并最终将它们发布到我自定义创建的 API 也是如此。似乎应用程序在 GPSTracker.java 中的位置管理器上崩溃

GPSTracker.java

package YOU.SOLD.YOUR.SOUL;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;

import androidx.annotation.Nullable;

public class GPSTracker extends Service implements LocationListener {

    LocationManager LM;

    @SuppressLint("MissingPermission")
    @Override
    public void onCreate() {
        LM.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 1, this);
    }

    @Override
    public void onDestroy() {

    }

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

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

主 Activity .java

package YOU.SOLD.YOUR.SOUL;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity {

    private static final int PERMISSION_1 = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_1);
        Intent intent = new Intent(this, GPSTracker.class);
        startService(intent);
    }
}

添加到 Manifest 的权限是 COARSE 和 FINE 位置。还添加了 .GPSTracker 服务

java.lang.RuntimeException: Unable to create service YOU.SOLD.YOUR.SOUL.GPSTracker: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.location.LocationManager.requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)' on a null object reference

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.location.LocationManager.requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)' on a null object reference

最佳答案

为了修复 NullPointerException,只需初始化 LocationManager 引用。 此外,将请求位置更新的逻辑移动到 onStartCommand() 方法重写,并返回 START_STICKY 以便自动重启服务。

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        LocationManager locationManager =
        (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 1, this);
    }

    return Service.START_STICKY;
}

关于java - 服务中的 Android 位置管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58032987/

相关文章:

java - 数组中的双哈希表示

java - 如何在 Java 中将日期/时间从长(毫秒)转换为 RFC-822 格式

java - com.facebook.react.bridge.WritableMap.copy() 在空对象引用上

Java 应用程序作为系统服务

linux - @reboot cron 作业的替代方案,在 cron 守护进程启动时启 Action 业

Java (Eclipse) 对我自己的 jar 库的引用无法解析

java - 在 method.invoke() 中捕获自定义异常

java - 在 recyclerview 适配器中使用字符串数组数据之前,将其转换为 arraylist

android - 如何在 Android 中使用 Eclipse 包含和使用 ZXing 库?

android - 如何减少 Android RecyclerVIew 中项目点击操作的延迟?