服务定时器延迟

时间:2018-04-05 08:40:52

标签: java android android-thread

我正在创建一个应用程序,它将在一个游戏中检查村庄的状态,我有一个问题。

我有后台服务,设定时间60秒,并重复。 一切都看起来不错,但是经过一段时间(10-30分钟)后,我延迟了背景计时器的申请。

例如: 执行 - 10:55:00 执行 - 10:57:10 执行 - 10:58:30 执行 - 10:59:50 执行 - 11:01:30 6.执行 - 11:03:00

这是登录应用程序的时间屏幕: Application Image

仅当我将应用程序最小化到后台时才会发生这种情况。当我在申请时,一切都很好。

实际服务

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

实际代码。当移动设备与充电器断开时,延迟开始。何时移动充电器一切正常,更新60秒。 一些有助于解决这个问题?

编辑6.4.2018 我现在的服务。

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();*/

//可选择设置自定义视图

    //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 /// 我的AlertManager但仅工作1分钟。

 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);
}

0 个答案:

没有答案