Étape 2: votre app (activité principale)
Import org.apache.http.client.HttpClient ;
Import org.apache.http.client.methods.HttpGet ;
Import org.apache.http.impl.client.DefaultHttpClient ;
Import android.app.Activity ;
Import android.os.Bundle ;
Import android.os.StrictMode ;
Import android.view.Menu ;
Import android.view.View ;
Import android.view.View.OnClickListener ;
Import android.widget.Toast ;
public class que mainactivity étend l’activité implémente {OnClickListener
protected void onCreate(Bundle savedInstanceState) {}
Politique de StrictMode.ThreadPolicy = new StrictMode.
ThreadPolicy.Builder().permitAll().build() ;
StrictMode.setThreadPolicy(policy) ;
super.onCreate(savedInstanceState) ;
setContentView(R.layout.main) ;
Découvre led1on = findViewById(R.id.led_1on) ;
Découvre led1off = findViewById(R.id.led_1off) ;
Découvre led2on = findViewById(R.id.led_2on) ;
Découvre led2off = findViewById(R.id.led_2off) ;
led1on.setOnClickListener(this) ;
led1off.setOnClickListener(this) ;
led2on.setOnClickListener(this) ;
led2off.setOnClickListener(this) ;
}
{} public boolean onCreateOptionsMenu (menu Menu)
Gonfler le menu ; Cela ajoute des éléments à la barre d’action si elle est présente.
getMenuInflater () .inflate (menu, R.menu.main) ;
retourne la valeur true ;
}
public void commandArduino (String url) {}
try {}
HttpClient httpclient = new DefaultHttpClient() ;
httpclient.Execute (new HttpGet(url)) ;
} catch (Exception e) {}
}
}
{} public void onClick (voir thisView)
{Switch(thisView.GetId())}
affaire R.id.led_1on :
commandArduino ("http://192.168.0.15/?cmd=1") ;
Toast.makeText(getApplicationContext(), « led_1on",Toast.LENGTH_LONG).show() ;
rupture ;
affaire R.id.led_1off :
commandArduino ("http://192.168.0.15/?cmd=2") ;
Toast.makeText(getApplicationContext(), « led_1off",Toast.LENGTH_LONG).show() ;
rupture ;
affaire R.id.led_2on :
commandArduino ("http://192.168.0.15/?cmd=3") ;
Toast.makeText(getApplicationContext(), « led_2on",Toast.LENGTH_LONG).show() ;
rupture ;
affaire R.id.led_2off :
commandArduino ("http://192.168.0.15/?cmd=4") ;
Toast.makeText(getApplicationContext(), « led_2off",Toast.LENGTH_LONG).show() ;
rupture ;
}
}
}