[General Questions and Answers] dantesprk:
Hi everyone. I have a weird problem with android service. I use 60 sec timer to output current datetime to file. I get a non constant time output. And the service unexpectedly ends after some time(about 0.5-1 hour). And I can't understand the reason. According to the latest changes i use startForegroundService. I tested it on android 8.1 (OnePlus5), android 6(Lenovo k3 note) and ginzzu(Android 5). Is it a bug or mistake I made?

Output file attached.

MainActivity.java

package com.example.yas.myapplicationtest;

import android.Manifest;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}

NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

int notificationId = 1;
String channelId = "channel-01";
String channelName = "Channel Name";

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
//int importance = ;
NotificationChannel mChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
}

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
startForegroundService(new Intent(this, MyService.class));
else
startService(new Intent(this, MyService.class));
}
}

MyService.java

package com.example.yas.myapplicationtest;

import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class MyService extends Service {
private Timer timer;
private TimerTask timerTask;
public int counter=0;

public void startTimer() {
timer = new Timer();

timerTask = new TimerTask() {
public void run() {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, "tmp.txt");

try
{
file.createNewFile();
FileOutputStream fOut = new FileOutputStream(file,true);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);

Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(" dd:MMMM:yyyy HH:mm:ss a");
final String strDate = simpleDateFormat.format(calendar.getTime());

myOutWriter.append(Integer.toString(counter) + strDate + "\n");
myOutWriter.close();

fOut.flush();
fOut.close();
}
catch (IOException e)
{
Log.e("Exception", "File write failed: " + e.toString());
}

}
};

timer.schedule(timerTask, 0, 60000);
}

@Override
public void onCreate() {
super.onCreate();

Toast.makeText(this,"Created", Toast.LENGTH_SHORT).show();

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel-01")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText("Notification text").setOnlyAlertOnce(true);

startForeground (45, builder.build());
startTimer();
}

public void onDestroy() {
super.onDestroy();
if (timer != null) {
timer.cancel();
timer = null;
}
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
//return super.onStartCommand(intent, flags, startId);
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}

Output^

0 10:August:2018 15:30:34 PM
0 10:August:2018 15:31:34 PM
0 10:August:2018 15:32:34 PM
0 10:August:2018 15:33:34 PM
0 10:August:2018 15:34:34 PM
0 10:August:2018 15:35:34 PM
0 10:August:2018 15:37:12 PM
0 10:August:2018 15:46:12 PM
0 10:August:2018 15:51:26 PM
0 10:August:2018 15:54:02 PM
0 10:August:2018 15:55:02 PM
0 10:August:2018 15:57:24 PM
0 10:August:2018 16:00:32 PM
0 10:August:2018 16:05:36 PM
0 10:August:2018 16:08:59 PM
0 10:August:2018 16:13:54 PM
0 10:August:2018 16:17:51 PM
0 10:August:2018 16:19:15 PM
0 10:August:2018 16:20:16 PM
0 10:August:2018 16:21:23 PM
0 10:August:2018 16:25:46 PM
0 10:August:2018 16:28:17 PM
0 10:August:2018 16:30:42 PM
0 10:August:2018 16:37:16 PM
0 10:August:2018 16:38:16 PM
0 10:August:2018 16:40:13 PM
0 10:August:2018 16:56:03 PM
0 10:August:2018 17:06:20 PM
0 10:August:2018 17:20:19 PM
0 10:August:2018 17:35:30 PM
0 10:August:2018 17:40:14 PM
0 10:August:2018 17:41:14 PM
0 10:August:2018 17:42:14 PM
0 10:August:2018 17:43:14 PM
0 10:August:2018 17:44:14 PM
0 10:August:2018 17:45:14 PM
0 10:August:2018 17:46:19 PM
0 10:August:2018 17:56:11 PM