Skip to content

Instantly share code, notes, and snippets.

@a-thomas
Created December 8, 2012 00:39
Show Gist options
  • Save a-thomas/4237852 to your computer and use it in GitHub Desktop.
Save a-thomas/4237852 to your computer and use it in GitHub Desktop.
Formation Android 1)
private class ParlezVousTask extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPreExecute() {
// exécuté dans l'UIThread
}
@Override
protected Boolean doInBackground(String... params) {
// exécuté sur un autre thread
return true;
}
@Override
protected void onPostExecute(Boolean result) {
// exécuté dans l'UIThread
}
}
// envoyer un broadcast
Intent intent = new Intent();
intent.setAction("com.excilys.formation.MY_OWN_BROADCAST");
sendBroadcast(intent);
try {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://www.google.com");
HttpResponse response = client.execute(request);
InputStream content = response.getEntity().getContent();
// ...
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Intent intent = new Intent(this, MyActivity.class);
startActivity(intent);
// ou
Intent intent = new Intent();
intent.setAction("com.excilys.formation.MY_OWN_ACTION");
startActivity(intent);
public class ParlezVousActivity extends Activity {
private final String TAG = ParlezVousActivity.class.getSimpleName();
public class MainActivity extends Activity {
private TextView myTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView = (TextView) findViewById(R.id.my_text_view);
myTextView.setText(R.string.hello_world);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Example</string>
<string name="hello_world">Hello world!</string>
</resources>
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int my_text_view=0x7f070000;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040000;
public static final int hello_world=0x7f040001;
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Juste un exemple, retient nombre
outState.putInt("NUMBER", 4);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// La nouvelle Activity créée peut récupérer ce nombre
int selectedIndex = savedInstanceState.getInt("NUMBER");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment