Junio 02, 2010 :: Posted by - Emilio Torrens :: Category -
Android,
Eclipse,
Java
Un par de cositas a tener en cuenta cuando programamos para Android con el Eclipse.
Se te generara una clase que hereda de Activity que es donde esta el meollo, allà tenemos acceso a todos los objetos de la aplicación y es donde pondremos el código.
1: public class MyAppActivity extends Activity {
2: /** Called when the activity is first created. */
3:
4: @Override
5: public void onCreate(Bundle savedInstanceState) {
6:
7: super.onCreate(savedInstanceState);
8: setContentView(R.layout.main);
9: }
10: }
Los nombres (formularios, imágenes, controles en los formularios … ) son únicos para toda la aplicación, se genera una clase R.java donde tendrás campos estáticos con todos los Ids agrupados en clases:
- layout, los ids de los formularios
- string, los ids de los recursos
- drawable, los ids de las imágenes
- id, los ids de los controles.
Para cargar los formularios usaremos el método setContentView pasándole el id del formulario que tenemos en la clase R.java
1: private void OnClickForm2() {
2: setContentView(R.layout.form2);
3: }
Para acceder a los controles utilizaremos el método FindViewById, usando los ids de la clase R.java, nos devolverá un objeto y hacemos cast al control.
1: TextView textoCabecera = (TextView) findViewById(R.id.TextoCabecera);
Para capturar los eventos en los controles tienes que hacerlo después de cargar el layout, accediendo al control por el Id.
1: setContentView(R.layout.main);
2:
3: try
4: {
5: this.Boton1 = (Button)this.findViewById(R.id.Boton1);
6: this.Boton1.setOnClickListener(new OnClickListener(){
7: @Override
8: public void onClick(View v) {
9: OnClickBoton1();
10: }
11: });
12:
13: this.Boton2 = (Button)this.findViewById(R.id.Boton2);
14: this.Boton2.setOnClickListener(new OnClickListener(){
15: @Override
16: public void onClick(View v) {
17: OnClickBoton2();
18: }
19: });
20: }
21: catch(Exception ex)
22: {
23:
24: }
Y por ultimo, enviar notificaciones al usuario, aquà dejo un método para eso, lo único a tener en cuenta es el NOTIF_ID que es global y se incrementa después de cada notificación.
1: public void Notify(String title, String text, int iconID)
2: {
3: String ns = Context.NOTIFICATION_SERVICE;
4: NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
5: int icon = iconID;
6: CharSequence tickerText = title;
7: long when = System.currentTimeMillis();
8:
9: Notification notification = new Notification(icon, tickerText, when);
10:
11: Context context = getApplicationContext();
12: CharSequence contentTitle = title;
13: CharSequence contentText = text;
14: Intent notificationIntent = new Intent(this, WindConditionsActivity.class);
15: PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
16:
17: notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
18:
19: mNotificationManager.notify(NOTIF_ID, notification);
20:
21: NOTIF_ID++;
22: }
Ahora no tengo mucho tiempo para programar Android, pero bueno a medida que descubra cosas nuevas las iré publicando