/** This class will invoke the method interface in "UPnP service Switchpower" on the "light bulb device under the LAN" to control the bulb switch **/
public class Switchactivity extends activity {Private button btnswitch;//switch button private Boolean ison;//bulb State Private Boolean flag=false;//whether to read the bulb's initial state
The Handler object is responsible for "passing information from a communication thread to the UI thread" to change the state of the switch private Handler Handler = new Handler () {
@Override public void Handlemessage (msg) {switch (msg.what) {case 1:if (ison = = True) {Btnswitch.settext ("off Lights"); else {btnswitch.settext ("lights On");} break; } }
};
@Override public void OnCreate (Bundle savedinstance) {
Super.oncreate (savedinstance); Setcontentview (R.layout.switch); Initcomponents ()///Pull button from layout file, omitted here
Read the bulb state to initialize the switch state btnswitch.setenabled (false); while (Flag==false) {GetStatus ();} btnswitch.setenabled (True); Set Switch Listener btnswitch.setonclicklistener (new Onclicklistener () {
@Override public void OnClick (View arg0) {if (ISON = = True) {SetStatus (false);/off Light} else {setstatus (true);//Turn on Light}
} });
}
Invokes the method interface "GetStatus" (upnpaction) defined in the UPnP service "Switchpowe" to get the bulb device state private void GetStatus () {
Take the Upnpaction interface from the service actioninvocation ai = new Actioninvocation (BrowserActivity.switchPowerService.getAction (" GetStatus "));
Perform asynchronous Communication task BrowserActivity.upnpService.getControlPoint (). Execute (New Actioncallback (AI) {
Returns successful execution @Override public void success (Actioninvocation arg0) {System.out.println ("getstatus success"); ison = (Boolean) Arg0.getoutput ("Resultstatus"). GetValue ()//Get the Bulb state handler.sendemptymessage (1) in the return value,//Notify UI to Change button status flag=true;// Button listening will be activated}
Returns execution failure @Override public void failure (Actioninvocation arg0,upnpresponse arg1, String arg2) {System.out.println (" GetStatus failure "); }
}); }
Invokes the method interface "SetStatus" (upnpaction) defined in the UPnP service "Switchpowe" to implement the remote control bulb switch private void SetStatus (Boolean arg) {
Take the Upnpaction interface from the service and set the parameters actioninvocation ai = new Actioninvocation (BrowserActivity.switchPowerService.getAction (" SetStatus ")); Ai.setinput ("Statusnewvalue", Arg);
Execute upnpaction (asynchronous) BrowserActivity.upnpService.getControlPoint (). Execute (New Actioncallback (AI) {
Returns the successful execution @Override public void success (Actioninvocation arg0) {System.out.println ("setstatus success"); Handler.sendemptymessage (1)//Notification UI Change button status}
Returns execution failure @Override public void failure (Actioninvocation arg0,upnpresponse arg1, String arg2) {System.out.println (" SetStatus failure "); }
});
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.