Android: How to enable a button when the service completes its task?

I have two buttons StartButton and StopButton on MainActivity.java. When I click StartButton the Service is created and the StartButton gets disabled . When StopButton is pressed , the Service will be destroyed and StartButton will get enabled .

What I wanted to do was turn on my Start button since the service had done its job; it returns any trigger type notification or event or message value on my MainActivity.java so my start button gets permission.

Please give me some idea, concept or code to make it easier for me to understand.

+3


source to share


3 answers


You tried,

MyButton.setClickable(false);

      

He will disable the button.

and

MyButton.setClickable(true);

      

He will turn on the button.

However, if you want to hide or show the button, you can use



MyButton.setVisibiity(VIEW.VISIBLE);

and MyButton.setVisibiity(VIEW.INVISIBLE);

Edit - . How do you want to enable the button, after completing its task, you can open mainActivity with the flag true, something like this,

Intent i= new Intent(getBaseContext(), myActivity.class);
i.putExtra("flag", true);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(i);

      

and in mainActivity , check the flag value,

Boolean flag;
flag = this.getIntent().getBooleanExtra("flag", false);
if(flag == true) {
MyButton.setClickable(true);
}

      

But in order to do this, you must first save the value of the flag to false

.

and once you click the button, you can set it as non-clickable.

0


source


To understand the relationship between a service and an app, see the examples in the Android developer docs . You can enable and disable your buttons in the onServiceDisconnected () and onServiceConnected () methods.



Keep in mind that the service runs on the same thread as the default application.

0


source


This can be a lot. Let me tell you one with a broadcast receiver. Hope you are familiar with the BroadcastReceiver concepts.

Changes in your business

1) Create broadcast receiver as an inner class in your activity.

2) Register it with some action, suppose "MY_ACTION". (don't forget to unregister this receiver on onPause / onDestroy methods)

3) Assuming the service has done its job and you are using the onReceive method of the broadcast receiver, now write your code to enable / disable the button (I hope you know how to do this)

Changes to your service

1) find the code by which you think his task is complete.

2) Create an intent with the same action ("MY_ACTION").

3) Broadcast message with sendBroadcast, intent as parameter. and you're done. :)

let me know if at some point you feel stuck,

0


source







All Articles