stips

Android: ejemplos de llamadas a Intents

Los filtros de Intents son, desde mi punto de vista, una de las características más potentes de Android. Nos permiten reutilizar componentes de otras aplicaciones usando muy pocas líneas de código.

“¿Para que voy a programar una Activity de captura de fotos cuando puedo llamar a la de la aplicación de cámara incluida en el sistema?”

Os dejo un ejemplo sencillo en el que se realizan varias llamadas para ilustrarlo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
 * Intent samples
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.francho.test.intents;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class IntentsActivity extends Activity {
    private static final int REQUEST_PHOTO = 1;
    private HashMap<String, Intent> mPruebas;
    private LinearLayout mContainer;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContainer = new LinearLayout(this);
        mContainer.setOrientation(LinearLayout.VERTICAL);
        mContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        setContentView(mContainer);
        Intent intent;
        // Web
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://francho.org/"));
        createButton("Web", intent);
        // Web filtrada
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/"));
        createButton("Google maps (web)", intent);
        // Geo (maps)
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:41.656313,-0.877351"));
        createButton("Google maps geopunto", intent);
        // Llamadas
        intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:976123123"));
        createButton("Llamada telefónica", intent);
        // Enviar texto (sencillo)
        intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, "asunto de prueba");
        intent.putExtra(Intent.EXTRA_TEXT, "probando el envio");
        createButton("compartir (selector)", intent.createChooser(intent, "enviar"));
        // enviar un mail
        intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, "asunto de prueba");
        intent.putExtra(Intent.EXTRA_TEXT, "probando el envio");
        intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"sheldon@cooper.com"});
        createButton("Mail To", intent);
        // tomar una foto
        intent = new Intent("android.media.action.IMAGE_CAPTURE");
        createButton("Tomar foto", intent, REQUEST_PHOTO);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == Activity.RESULT_OK) {
            Bundle extras;
            switch(requestCode) {
            case REQUEST_PHOTO:
                extras = data.getExtras();
                String result = data.toURI();
                Toast.makeText(IntentsActivity.this, result, Toast.LENGTH_LONG).show();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    protected void createButton(String label, Intent intent) {
        createButton(label, intent, null);
    }
    protected void createButton(String label, Intent intent, Integer requestCode) {
         Button btn = new Button(this);
         btn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
         btn.setText(label);
         btn.setOnClickListener(new BtnOnClickListener(intent, requestCode));
         mContainer.addView(btn);
    }
    class BtnOnClickListener implements OnClickListener  {
        private Intent mIntent;
        private Integer mRequestCode;
        BtnOnClickListener(Intent intent, Integer requestCode) {
            mIntent = intent;
            mRequestCode = requestCode;
        }
        public void onClick(View v) {
            try {
                if( mRequestCode != null) {
                    startActivityForResult(mIntent, mRequestCode);
                } else {
                    startActivity(mIntent);
                }
            } catch(Exception e) {
                Toast.makeText(IntentsActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }
    }
}

 

Subido el código fuente de este proyecto a Github: https://github.com/francho/francho.org-lab/tree/master/0806-LlamadasIntents