Escolar Documentos
Profissional Documentos
Cultura Documentos
3 - Intents
3 - Intents
Intent
startActivity ( intent );
3
Intent Filter
Action = MAIN
Diz que activity um ponto de partida, semelhante ao public static void main()
da Java
Category = LAUNCHER
Indica que o cone da activity dever aparecer na tela pode ser aberta pelo
usurio
6
Exemplo
public class MainActivity extends Activity {
@Override
public void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView (R. layout . main );
Button viewSiteButton = ( Button ) findViewById (R.id. view_site_button );
Button sendEmailButton = ( Button ) findViewById (R.id. send_email_button );
Button makeCallButton = ( Button ) findViewById (R.id. make_call_button );
viewSiteButton . setOnClickListener ( new OnClickListener () {
@Override
public void onClick ( View v) {
Intent intent = new Intent ( Intent . ACTION_VIEW , Uri
. parse ("http://ufu.br"));
startActivity ( intent );
}
});
Exemplo
Action
Descrio
ACTION_VIEW
http://www.gmail.com
ACTION_VIEW
ACTION_EDIT
content://com.android.contacts/contacts/1
ACTION_VIEW
geo:0,0?q=Presidente+Vargas,Rio+de+Janeiro
ACTION_CALL
ACTION_DIAL
tel:12345678
ACTION_INSERT
content://com.android.contacts/contacts
ACTION_PICK
ContactsContract.Contacts.CONTENT_URI
ACTION_SET_WALLPAPER
10
Aes de Intent
Exemplo:
Uri uri = Uri.parse("content://com.android.contacts/contacts");
Intent intent = new Intent(Intent.ACTION_INSERT, uri);
startActivity(intent);
intent = new Intent(Intent.ACTION_SET_WALLPAPER);
startActivity(intent);
Experimente tambm:
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "Maria");
startActivity(intent);
Sim, voc pode passar parmetros via Intent, dependendo de qual ao est sendo
executada!
11
Aes de Intent
Alm disso, para usar o Google Maps necessrio rodar o emulador com Google
Maps.
android.permission.READ_CONTACTS
android.permission.VIBRATE
android.permission.INTERNET
android.permission.BATTERY_STATS
android.permission.BLUETOOTH
android.permission.CAMERA
android.permission.REBOOT
android.permission.SEND_SMS
12
possvel adicionar tantas categorias e parmetros extras quanto forem necessrios, alm
de outros dados:
Mtodo da Intent
Descrio
addCategory(String)
putExtra(String, ...)
setData(Uri)
setType(String)
setFlags(int)
Define os flags da Intent. Um flag til o FLAG_ACTIVITY_NEW_TASK, que faz a activity
addFlags(int)
numa
tarefa ou seja,
13
Tecla Home
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Intent.ACTION_SEARCH
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
Intent.ACTION_WEB_SEARCH
<action android:name="android.intent.action.WEB_SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
14
Aes nativas
intent.putExtra(SearchManager.QUERY, "Maria");
startActivity(intent);
Execute IntentFilterTest
Veja que o emulador ir perguntar
qual aplicao executar
15
Filtragem
Ao executar uma Intent, uma activity s rodar caso seu intent-filter satisfaa a
ao e todas as categorais definidas na Intent.
Exemplos:
1.
2.
3.
16
No manifest de IntentFilterNativeSubst
Em IntentFilterTest
17
Exemplo: com.example.application.ActivityName
<uses-permission android:name="application_package.PERMISSION_NAME"/>
19
Widgets
EditText
setOnKeyListener()
setOnClickListener()
setOnFocusChangedListener()
setOnFocusChangedListener()
simpleEditText.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus) {
String strValue = simpleEditText.getText().toString();
21
Log.d(DEBUG_TAG, "User set EditText
value to " + strValue);
}
});
EditText
none
textEmailAddress
Diferentes tipos de
entrada
text
textEmailSubject
android:inputType="phone
textCapCharacters
textShortMessage
textCapWords
textLongMessage
textCapSentences
textPersonName
android:inputType="textPassword
textAutoCorrect
textPostalAddress
textAutoComplete
textPassword
textMultiLine
Definir SugestestextVisiblePassword
(Hints) e valores
textImeMultiLine
textWebEditText
android:hint="@string/hint"
textNoSuggestions
textFilter
textUri
textPhonetic
textWebEmailAddress
textWebPassword
number
numberSigned
numberDecimal
numberPassword
phone
datetime
dos
EditText
date
tim
22
EditText
Limitar
a zeros e uns
android:digits="01
Desabilitar
o cursor
android:cursorVisible="false"
23
x)
24
);
}
25
Cdigo do OnTextChanged()
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!editando) {
editando = !editando;
String text = et.getText().toString();
fone = text.replaceAll("\\(", "").replaceAll("\\)", "").replaceAll("-", "").replaceAll(" ", "");
if (fone.length() >= 2 && fone.length() > tamVelho) {
String parte0 = fone.substring(0, 2);
String parte1 = fone.substring(2);
String parte2 = "";
if (fone.length() < 6) {
tamVelho = fone.length();
fone = "(" + parte0 + ") " + parte1;
} else {
parte1 = fone.substring(2, 6);
parte2 = fone.substring(6);
tamVelho = fone.length();
fone = "(" + parte0 + ") " + parte1 + "-" + parte2;
}
} else {
tamVelho = fone.length();
fone = text;
}
if (fone.length() > 2) {
et.setTextKeepState(fone, BufferType.EDITABLE);
et.setSelection(fone.length());
}
editando = !editando;
}
}
26
Button e ImageButton
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
... />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_icon"
... />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
27
android:drawableLeft="@drawable/button_icon"
... />
Button e ImageButton
imageButton.setImageResource(R.drawable.image_id)
button.setCompoundDrawablesWithIntrinsicBounds(left,top,right,bottom)
<Button android:drawableLeft="@drawable/..."
android:drawableTop="..."
android:drawableRight="..." android:drawableBottom="..." ... />
Detectando clique:
29
bStart1.setOnClickListener(this);
bStart2.setOnClickListener(this);
bSair.setOnClickListener(this);
30
Ou ento no XML
android:onClick="buttonClicked"
armazenando a referncia
implementando OnClickListener e
Exerccios