我想创建一个应用程序,该应用在上午9:00至9:00 pm之间每5分钟获取用户的位置。现在我无法思考流程。我在上感到困惑:
- 我应该实现2个重复警报管理器,一个每5分钟,另一个用于时间插槽。?
- 或以某种方式进行一次,每5分钟发射警报,然后检查它是否在时间插槽之间,然后仅运行位置服务并上传到服务器工作。?
请帮助我提供建议/建议。如何在手机电池,效率方面以最佳方法实现这一目标。
您不应使用handler.postdelayer((时间间隔,超过30秒,因为它可能导致内存泄漏。与AlarmManager(较长的间隔(制定了几个策略 - 一种短时间间隔 - 使用处理程序和另一种策略。
请参阅https://codelabs.developers.google.com/codelabs/background-location-location-pocation-updates-and-htroid-o/index.html?index=../../../index#0
您可以将更新间隔设置为位置请求中的5分钟。它与Android" O"兼容。
在这里我创建了一个服务,该服务以1分钟的间隔提供当前位置。您可以根据您的要求对其进行修改 -
还将其添加到Gradle文件 -
中compile 'com.google.android.gms:play-services:9.4.0'
locationservice.class
import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import java.text.SimpleDateFormat;
import java.util.Date;
public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener, ActivityCompat.OnRequestPermissionsResultCallback {
protected static final String TAG = "location-updates";
private static final long day = 24 * 60 * 60 * 1000;
private static final long hours = 60 * 60 * 1000;
private static final long minute = 60 * 1000;
private static final long fiveSec = 5 * 1000;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
@Override
public void onCreate() {
super.onCreate();
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(minute);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setSmallestDisplacement(0);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
@Override
public void onConnected(Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
//Location Listener
@Override
public void onLocationChanged(final Location location) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDateandTime = sdf.format(new Date());
Log.e("Location ", location.getLatitude() + " " + location.getLongitude() + " " + currentDateandTime);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
boolean isGranted = false;
for (int i = 0; i < grantResults.length; i++)
if (permissions[i].equals(Manifest.permission.ACCESS_FINE_LOCATION) && (grantResults[i] == PackageManager.PERMISSION_GRANTED))
isGranted = true;
if (isGranted) {
startService(new Intent(this, LocationService.class));
} else {
}
}
}
好吧,您可以使用处理程序查询位置,您可以执行此类操作
Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
currentLocation = getCurrentUserLocation();
handler.postDelayed(this, 1000*60*5);
}
}
};
handler.postDelayed(runnable, 1000*60*5);
并且要检查时间间隔,您可以为此任务设置警报管理器
,一旦达到时间限制,您需要通过
删除处理程序回调handleLocation.removeCallbacksAndMessages(null);