我正在创建一个应用程序,它将在一场游戏中检查村庄的状态,但我遇到了问题。
我有设置时间为 60 秒的后台服务,并且重复。一切看起来都很好,但是经过一段时间(10-30 分钟)后,我在后台计时器上延迟了应用程序。
例如: 1. 执行 - 10:55:00 2. 执行 - 10:57:10 3. 执行 - 10:58:30 4. 执行 - 10:59:50 5. 执行 - 11:01:30 6.执行 - 11:03:00
这是应用程序登录时间的屏幕: 应用程序图像
仅当我将应用程序最小化到后台时才会发生这种情况。当我在申请时,一切都很好。
实际服务
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
实际代码。当手机与充电器断开连接时,延迟开始。什么时候可以在充电器上移动,一切正常并更新 60s。一些帮助解决这个问题?
编辑 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;
}
编辑/// 我的 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);
}