1 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊
為了修復(fù) NullPointerException,只需初始化 LocationManager 引用即可。此外,將請(qǐng)求位置更新的邏輯移至方法onStartCommand()重寫,然后返回START_STICKY以便自動(dòng)重新啟動(dòng)服務(wù)。
@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;
}
添加回答
舉報(bào)