Intent - nativas do Android

Top  Previous  Next

3 exemplos em 1:

Exemplo 1 - abrindo página da web

 

package com.intencao;

 

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

 

public class IntencaoActivity extends Activity implements OnClickListener {

   

  @Override

  public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

       

      final Button btAbrir = (Button) findViewById(R.id.btAbrir);

       btAbrir.setOnClickListener(this);

   }

 

 public void onClick(View v) {

         EditText edURL = (EditText) findViewById(R.id.edURL);

         

         String url = edURL.getText().toString(); // deve ter http:// - ex: http://www.google.com.br

         

         // monta o URI

         Uri uri = Uri.parse(url);

         

         // cria o intent com o endereço

         Intent intencao = new Intent(Intent.ACTION_VIEW, uri);

         

         // inicia a activity

         startActivity(intencao);

 }

}

 

main.xml

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent"

   android:orientation="vertical" >

 

   <TextView

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

      android:text="Digite URL e pressione o botão" />

 

  <EditText

       android:id="@+id/edURL"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content" >

 

       <requestFocus />

   </EditText>

 

   <Button

       android:id="@+id/btAbrir"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

      android:text="Abrir" />

 

</LinearLayout>

 

AndroidManifest.xml

 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

   package="com.intencao"

   android:versionCode="1"

   android:versionName="1.0" >

 

   <uses-sdk android:minSdkVersion="7" />

  <uses-permission android:name="android.permission.INTERNET"/>

 

   <application

       android:icon="@drawable/ic_launcher"

       android:label="@string/app_name" >

       <activity

           android:name=".IntencaoActivity"

           android:label="@string/app_name" >

           <intent-filter>

               <action android:name="android.intent.action.MAIN" />

 

               <category android:name="android.intent.category.LAUNCHER" />

           </intent-filter>

       </activity>

   </application>

 

</manifest>

 


Exemplo 2 - exibindo contatos

 

// Exibindo TODOS os contatos

Uri uri = Uri.parse("content://com.android.contacts/contacts/");

Intent intencao = new Intent(Intent.ACTION_VIEW, uri);

startActivity(intencao);

 

// Exibindo dados do contato 1

Uri uri = Uri.parse("content://com.android.contacts/contacts/");

Intent intencao = new Intent(Intent.ACTION_VIEW, uri);

startActivity(intencao);

 

Não esqueça as permissões
 

   <uses-permission android:name="android.permission.READ_CONTACTS"/>

 


Exemplo 3 - fazendo uma ligação

 

Uri uri = Uri.parse("tel:123");

Intent intencao = new Intent(Intent.ACTION_CALL, uri);

startActivity(intencao);

 

Não esqueça as permissões
 

   <uses-permission android:name="android.permission.CALL_PHONE"/>