我需要android后台服务,该服务每天两次或一天三次在给定时间获取用户GPS位置,即使该应用已关闭或设备重启,该服务也应始终运行,我已在我的应用中成功添加了服务,从而使我可以位置更改,但是我无法仅在某些特定时间获得位置,例如[02:00, 05:30, 21:00]
这是我用来跟踪用户位置的服务
public class LocationService extends Service implements LocationListener {
LocationManager m_locationManager;
@Override
public void onCreate() {
this.m_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Toast.makeText(getApplicationContext(), "Location Service starts", Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//TODO do something useful
Toast.makeText(getApplicationContext(), "Service starts", Toast.LENGTH_SHORT).show();
// Here I offer two options: either you are using satellites or the Wi-Fi services to get user's location
// this.m_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 0, this); // User's location is retrieve every 3 seconds
this.m_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
//return TODO;
}
this.m_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
//TODO for communication return IBinder implementation
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(), "Service Task destroyed", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(getApplicationContext(), LocationService.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);
AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 01); // For 1 PM or 2 PM
calendar.set(Calendar.MINUTE, 43);
calendar.set(Calendar.SECOND, 0);
alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(getApplicationContext(), "Start Alarm", Toast.LENGTH_SHORT).show();
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Intent myIntent = new Intent(getApplicationContext(), LocationService.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);
AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(getApplicationContext(), "Start Alarm", Toast.LENGTH_SHORT).show();
}
@Override
public void onLocationChanged(Location loc) {
if (loc == null) // Filtering out null values
return ;
Double lat = loc.getLatitude();
Double lon = loc.getLongitude();
// Toast.makeText(getApplicationContext(),"Latitude = "+lat+ "\nLongitude = "+lon, Toast.LENGTH_SHORT).show();
// new UpdateLatitudeLongitude(LocationService.this, lat, lon,).execute();
//Calling AsyncTask for upload latitude and longitude
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}