I am creating an application which will check status of the villages in one game and i have a problem.
I have background service which have set time 60s and it's repeat. Everything looks fine but after some time (10-30 minutes) when i give application on background timer delayed.
for example: 1. execute - 10:55:00 2. execute - 10:57:10 3. execute - 10:58:30 4. execute - 10:59:50 5. execute - 11:01:30 6. execute - 11:03:00
This is screen of times log in application: Application Image
This occurs only when i minimize the application to background. When i am in application, everything is fine.
Actual Service
private ArrayList<Village> villages=null;
private boolean isNotificationEnable=true;
private boolean result;
public Context context = this;
public Handler handler = null;
public static Runnable runnable = null;
Thread thread;
private volatile boolean running = true;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
runnable = new Runnable() {
public void run() {
while(true) {
if (!running) return;
if (updateVillages()) {
result = true;
if (isNewVillage()) {
if (isNotificationEnable) doNotificate();
}
System.out.println("Prebehlo overovanie");
} else {
result=false;
System.out.println("Není internetové pripojenie");
}
updateUI();
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread= new Thread(runnable);
thread.start();
}
@Override
public void onDestroy() {
/* IF YOU WANT THIS SERVICE KILLED WITH THE APP THEN UNCOMMENT THE FOLLOWING LINE */
//handler.removeCallbacks(runnable);
Toast.makeText(this, "Kontrolovanie dedín bolo vypnuté", Toast.LENGTH_LONG).show();
running=false;
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "Kontrolovanie dedín bolo zapnuté", Toast.LENGTH_LONG).show();
}
//MyCode
Actual code. The delay started when mobile is disconnect from charger. When is mobile on charger everything work fine and updating 60s. Some help to fix this?
EDIT 6.4.2018 My service now.
ublic class BackgroundService extends Service {
private ArrayList<Village> villages=null;
private boolean isNotificationEnable=true;
private boolean result;
public Context context = this;
public Handler handler = null;
public static Runnable runnable = null;
Thread thread;
private volatile boolean running = true;
private Notification notification;
private MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakelock= pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getCanonicalName());
wakelock.acquire();
/*Intent intent = new Intent(this, Menu.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_warning_black_24dp);
builder.setTicker("App info string");
builder.setContentIntent(pi);
builder.setOngoing(true);
builder.setOnlyAlertOnce(true);
notification = builder.build();*/
// optionally set a custom view
//startForeground(2, notification);
/*player = MediaPlayer.create(this, Settings.System.DEFAULT_NOTIFICATION_URI);
player.setLooping(true);
player.start();*/
//handler=new Handler();
runnable = new Runnable() {
public void run() {
while(true) {
if (!running) return;
if (updateVillages()) {
result = true;
if (isNewVillage()) {
if (isNotificationEnable) doNotificate();
}
System.out.println("Prebehlo overovanie");
} else {
result=false;
System.out.println("Není internetové pripojenie");
}
updateUI();
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
//handler.postDelayed(runnable, 60000);
thread= new Thread(runnable);
thread.start();
}
@Override
public void onDestroy() {
/* IF YOU WANT THIS SERVICE KILLED WITH THE APP THEN UNCOMMENT THE FOLLOWING LINE */
//handler.removeCallbacks(runnable);
Toast.makeText(this, "Kontrolovanie dedín bolo vypnuté", Toast.LENGTH_LONG).show();
running=false;
//player.stop();
//stopForeground();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "Kontrolovanie dedín bolo zapnuté", Toast.LENGTH_LONG).show();
//startForeground(2, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO do something useful
return START_STICKY;
}
EDIT/// My AlertManager but work only for 1 min interval.
public void startAlert() {
int timeInSec = 2;
Intent intent = new Intent(this, BackgroundService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 50000,
intent, 0);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + 1000, 5000, pendingIntent);
}