0

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,

swpalmer
  • 3,890
  • 2
  • 23
  • 31
sagie uzan
  • 13
  • 2
  • There is a lot of missing context to your code. Only from the tag that shows at the bottom did I know you were talking about Android (because I got here via the Java subject, and I'm not familiar with Android classes). Showing import statements and mentioning a game engine if you are using one, would help. – swpalmer Sep 27 '20 at 03:02

1 Answers1

0

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;
Sid Shinde
  • 238
  • 3
  • 8
  • 1
    thank you very much it worked, the only problem is that the player can change his phone time and pretend he was offline for a longer period for goods. – sagie uzan Sep 30 '20 at 20:20
  • Yes this is a common loophole which players exploit in many games. To prevent this you need to use Network Time instead of System Time. Read this link for reference. https://stackoverflow.com/questions/8049912/how-can-i-get-the-network-time-from-the-automatic-setting-called-use-netw – Sid Shinde Oct 01 '20 at 07:00