我想运行前台服务
,无论应用程序是关闭还是打开,比如说 10 秒。 10 秒过去后,前台服务应被销毁,并在 5 分钟后再次调用。我的前台服务找到用户的位置,然后将其保存到 SQLite 数据库。所以基本上我想每 5 分钟存储一次新坐标,这样我就可以改善电池消耗。
我的服务等级:
public class LocationService extends Service{
private final static int UPDATE_TIME = 10000;
private final static int UPDATE_DISTANCE = 0;
private LocationListener locationListener;
private LocationManager locationManager;
private PowerManager.WakeLock mWakeLock;
private String latitude, longitude;
private DatabaseHelper myDb;
@Override
public void onCreate() {
super.onCreate();
myDb = new DatabaseHelper(this);
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
createWakeLock(); //Creates a wakelock
createNotification(); //Creates a notification if SDK version is > 26
}
@SuppressLint("MissingPermission")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mWakeLock.acquire(10*60*500L /*5 minutes*/);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.d("Location", "Updated");
getCoordinates(location); //Gets coordinates
insertCoordinates(latitude, longitude); //Sends coordinates to SQLite database
sendDataToMainActivity(latitude, longitude); //Sends coordinates to MainActivity
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, UPDATE_TIME, UPDATE_DISTANCE, locationListener);
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("Service", "Destroyed");
if(locationManager != null)
locationManager.removeUpdates(locationListener);
mWakeLock.release();
}
private void createWakeLock() {
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
if(pm != null)
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "myApp:myWakeLock");
}
private void createNotification() {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Service")
.setContentText("Coordinates Location Running")
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
}
private void getCoordinates(Location location) {
this.latitude = String.valueOf(location.getLatitude());
this.longitude = String.valueOf(location.getLongitude());
}
private void insertCoordinates(String latitude, String longitude) {
boolean inserted = myDb.insertData(latitude, longitude); //Insert coordinates
//Check if insertion is completed
if(inserted)
Toast.makeText(LocationService.this, "Coordinates Inserted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(LocationService.this, "Coordinates Not Inserted", Toast.LENGTH_SHORT).show();
}
private void sendDataToMainActivity(String latitude, String longitude) {
Intent i = new Intent("location_update");
i.putExtra("latitude", latitude);
i.putExtra("longitude",longitude);
sendBroadcast(i);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
最佳答案
使用 Fused Location Api 请求位置更新,这将在指定的重复间隔(给定 5 分钟)内触发待处理的 Intent(比方说启动 Intent 服务)。因此 Fused Location Api 每 5 分钟读取一次位置并将其发送到意向服务,您可以在其中进行处理。
请引用this了解更多详情。
关于android - 如何每 5 分钟运行一个前台服务 10 秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58797621/