We provide complete mobile and web apps development solutions

Wednesday, June 13, 2012

Intent in android

Intents:

        Intent is an abstract description of operation to be performed.
    Intents are asynchronous messages which allow android components to request functionality from other components in the android system and it is also used to pass the control from one activity to another activity.

Intents are 2 types:
Explicit intents
Implicit intents
Explicit intents:

Explicit intents:
If we want to navigate control from one activity to another activity, we have to use explicit intents.

Intent i=new Intent(context c,class);
startActivity(i);


Download source code
http://www.4shared.com/rar/VseQTmEN/Intent.html

startActivityForResult():

it supports only for bi directional communication.

How to use this? You want to learn.... lets see this example

package com.example.intentresult;
//Srinivas Nidadavolu    //     www.mobengineers.com   //
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
   
    public void GoNext(View v)
    {
       
        Intent i=new Intent(MainActivity.this,SecondActivity.class);
        startActivityForResult(i, 1); 
    }
}

activity_main.xml



why i have written startActivityForResult(i,1);

you know intent class is used to navigate from one activity to other activity. But, if we want to goback to previous activity, we have to get the result and pass it to next activity, with that result, we can move to back activity.

why i have written 1 as second parameter.

i.e  id of the intent. we can give any number here.

0 or 1 or 2 or 101 or 200 or..................... any number, not only these.

 


<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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="163dp"
        android:text="Button"
        android:onClick="GoNext"
         />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="31dp"
        android:text="Hello This is the first screen"
        android:textSize="25sp" />

</RelativeLayout>

 package com.example.intentresult;
//Srinivas Nidadavolu    //     www.mobengineers.com   //
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class SecondActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
    }
  
    public void GoBack(View v)
    {
        Intent i=new Intent();
        setResult(RESULT_OK,i);
        finish();
    }
   }

second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second Screen"
        android:textSize="25sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="GoBack"
        android:text="Go Back" />

</LinearLayout>




Ex:
public class MainActivity extends Activity {
    EditText et;
    String name;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
     et=(EditText)findViewById(R.id.editText1) ;
       
    }
   
    public void goToWelcome(View v)
    {
        if(et!=null && et.getText().length()!=0)
        {
        name = et.getText().toString();  
        }
        else
        {
            name="guest";
        }
        Intent i=new Intent(MainActivity.this,NextAct.class);
        i.putExtra("username", name);
        startActivity(i);
    }
}
package com.example.intent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class NextAct  extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.next);
        TextView tv=(TextView)findViewById(R.id.textView1);
       
        Intent i=getIntent();
        String usertext=(String) i.getSerializableExtra("username");
       
        tv.setText(usertext);
    }
   
   }
main.xml
<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginRight="25dp"
        android:layout_marginTop="26dp"
        android:text="Enter Username" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="26dp"
        android:layout_marginTop="26dp"
        android:hint="Enter Username"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginRight="77dp"
        android:layout_marginTop="43dp"
       
        android:onClick="goToWelcome"
        android:text="Go" />

</RelativeLayout>
next.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="104dp"
        android:layout_marginTop="97dp"
        android:text="TextView" />

</RelativeLayout>

 


Implicit Intents:
If we want to interact with default software components and hardware components, we have to use implicit intents.

for opening browser


Intent i=new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.mobengineers.com"));
startActivity(i);

we have to declare internet permission in manifest file.

for  calling number

Intent i=new Intent(Intent.ACTION_CALL,Uri.parse("tel:9848012345"));
startActivity(i);

we have to declare permission for call in manifest file.
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>


for opening camera
Intent i=new Intent("android.media.action.IMAGE_CAPTURE"));
startActivityForResult(i,0);


   




4 comments:

  1. Nice blog. Model of application development is changing to App based.Android is most popular OS for smart phones and all the businesses want to have their own Apps for Android.I wish you luck as you continue to follow that passion.

    ReplyDelete
  2. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
    hadoop-training-institute-in-chennai

    ReplyDelete
  3. I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.

    oracle training in Bangalore

    ReplyDelete
  4. Hi Admin. It has clear fiction about intent of Android mobiles. Now I am clearly understand. Thank you.

    Best Android Training in Chennai
    Best Android Training Institute in Chennai

    ReplyDelete

Online Training

Your Name :
Your Email: (required)
Your Message: (required)

Powered by Blogger.

Recent Posts

Find Us On Facebook

Popular Posts