Skip to content Skip to sidebar Skip to footer

Maintaining A Time-based Incrementing Float Whilst The App Is Paused Or Closed

I am developing an android game in which a global variable must gradually increment over time, with a varying increment amount. This variable will act as the player's 'resources',

Solution 1:

Services don't come up with any UI so you will have to bind them/or use broadcast with activites.which in turn display the values in the UI after communicating with each other .To start a service from an activity .

serviceIntent = newIntent(this, Music_service.class);

serviceIntent.putParcelableArrayListExtra("sentAudioLink", songdetails);//here "sentAudioLink //is the key which needs to be same in the service ,so that you can send 
        serviceIntent.putExtra("postion_service", position);


            startService(serviceIntent);

now the service has started....

inside the service read the data which had been passed earlier

songdetails = intent.getParcelableArrayListExtra("sentAudioLink");
        position = intent.getIntExtra("postion_service", 0);
        //note "sentAudioLink",its the key value,or the identifier which remains the same on sending and receiving

But sending data using the above method just sends the value only once that is during the start of the service,but in your case you would constantly want to send and receive the data....

so inside the service whatever you are calculating needs to be constantly sent to the activity so that UI can be updated and other things can be done ,in that case you will also like to use a broadcast

public staticfinal String BROADCAST_BUFFER = "source.justanothermusicplayer.broadcastbuffer";

bufferIntent =new Intent(BROADCAST_BUFFER);;//You can put anyvalueinto Broadcast_Buffer ,it is just a unique identifier foreach broadcast
andthen now that you hav initialized the intent its timeto put valuesinto it and send them using a broadcast

///

bufferIntent.putExtra("buffering", "2");
        sendBroadcast(bufferIntent);

put the above two lines inside a method which is called whenever a change happens,say when a button is clicked or timerischanged for e.g

Ontimerchanged(){

bufferIntent.putExtra("buffering", thevalueoftimer);//buffering is the key again...sendBroadcast(bufferIntent);
}

now go inside the activity again register the broadcast inside the activity

registerReceiver(broadcastReceiver, new IntentFilter(
                    Music_service.BROADCAST_BUFFER));//remeber the "BROADCAST_BUFFER" we created in the service??this is the same ...if you use BROADCAST_BUFFER2 or something like that or any other thing,it won't work.

now create this method

privateBroadcastReceiverbroadcastReceiver=newBroadcastReceiver() {
        @OverridepublicvoidonReceive(Context context, Intent serviceIntent) {
            Anymethod(serviceIntent);//serviceIntent has the values that are being passed...in your case value of timer etc.recieve the values in the method//TODO 
        }
    };

//

 AnyMethod(){
            //receiving the values here......Stringvalueoftimer= serviceIntent.getStringExtra("buffering");//again the key needs to be same//now that you have received the values ,you can set them up in a textview....

}

if my answer was helpful upvote and accept it please

Post a Comment for "Maintaining A Time-based Incrementing Float Whilst The App Is Paused Or Closed"