当应用关闭或被发送到后台时,如何运行计数器?

huangapple go评论69阅读模式
英文:

How do I run a counter when the app is closed or sent to the background?

问题

我正在制作一个游戏,游戏中蚂蚁会收集食物,我试图计算用户离线的时间,以便我可以计算出蚂蚁本应该收集到的食物数量。我尝试运行一个计时任务(timertask)和 looper 来在 onDstroy 中计算时间,但没有成功。我对其他想法持开放态度。

@Override
public void onCreate() {
    super.onCreate();
    player = MediaPlayer.create(this, R.raw.music);
    player.setLooping(true); // 设置循环播放
    player.setVolume(left, right);

    SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
    offlineTime = prefs.getInt("offline", 0);
    onTime = prefs.getInt("ont", 0);
    totalTime = prefs.getInt("total", 0);

    offlineTime = totalTime - onTime;

    intent = new Intent(BackgroundSoundService.this, LandingActivity.class);
    intent.putExtra("offline", offlineTime);
    startActivity(intent);
    offlineTime = 0;
    totalTime = 0;
    onTime = 0;
}

public int onStartCommand(final Intent intent, int flags, int startId) {
    player.start();
    stopMuOn = true;
    return Service.START_STICKY;
}

public void onStart(Intent intent, int startId) {
    // 待办事项
    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
            totalTime++;
            SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putInt("total", totalTime);
            editor.apply();
        }
    }, 100); // 毫秒,1000 毫秒 = 1 秒
}

public IBinder onUnBind(Intent arg0) {
    // 待办事项 自动生成的方法
    return null;
}

public void onStop() {

}

public void onPause() {
    player.pause();
}

@Override
public void onDestroy() {
    SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putInt("ont", onTime);
    editor.apply();
    player.stop();
    player.release();
}

提前感谢您。

英文:

I'm working on a game where ants collect food and I'm trying to get the amount of time that the user was offline so I can calculate the amount of food the ants should have had collected. I have tried to run a timertask and looper to count the time in onDstroy but it didn't work. I'm open to some other ideas.

  @Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.music);
player.setLooping(true); // Set looping
player.setVolume(left,right);
SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
offlineTime = prefs.getInt("offline",0);
onTime = prefs.getInt("ont",0);
totalTime = prefs.getInt("total",0);
offlineTime = totalTime - onTime;
intent = new Intent(BackgroundSoundService.this, LandingActivity.class);
intent.putExtra("offline", offlineTime);
startActivity(intent);
offlineTime = 0;
totalTime = 0;
onTime = 0;
}
public int onStartCommand(final Intent intent, int flags, int startId) {
player.start();
stopMuOn = true;
return Service.START_STICKY;
}
public void onStart(Intent intent, int startId) {
// TO DO
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
totalTime++;
SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("total",totalTime);
editor.apply();
}
}, 100); // Millisecond 1000 = 1 sec
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
public void onStop() {
}
public void onPause() {
player.pause();
}
@Override
public void onDestroy() {
SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("ont",onTime);
editor.apply();
player.stop();
player.release();
}

thanks in advance,

答案1

得分: 0

你在应用关闭时无需设置定时器。由于应用关闭时定时器会被销毁,所以也不能设置定时器。

你只需要像这样获取系统时间:

long time = System.currentTimeMillis();

在用户退出时,将当前系统时间存储在共享首选项中。
当用户再次联机时,从首选项中获取保存的时间(savedTime),并与新获取的当前时间进行比较。

long currentTime = System.currentTimeMillis();
    
long elapsedTime = currentTime - savedTime;
英文:

You do not need to set a timer when your app is closed. And you cannot set a timer because it will get destroyed when app closes.

You simply need to get the system time like this

long time = System.currentTimeMillis();

Store this current system time in shared preferences when user exits.
And when the user comes back online get the savedTime from preferences and compare with freshly fetched currentTime.

long currentTime = System.currentTimeMillis();
long elapsedTime = currentTime - savedTime;

huangapple
  • 本文由 发表于 2020年9月27日 05:51:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64082835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定