Friday 18 October 2013

What are Intent ?


An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.


An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.


Simple example of Intent

activity_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" >
 <Button
 android:id="@+id/btnGallery"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Show My Gallery" />
 <Button
 android:id="@+id/btnCallLog"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Show My Call Log" />
 <Button
 android:id="@+id/btnContact"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Show My Contacts" />
 <Button
 android:id="@+id/btnSearch"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Show My Search" />
</LinearLayout>


MainActivity.java

package in.androiddevelopmentanddiscussion.simpleintentexample;


import android.os.Bundle;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity {

Button GalleryBtn,CallLogBtn,ContactBtn,SearchBtn;


@Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
 
GalleryBtn=(Button)findViewById(R.id.btnGallery);
CallLogBtn=(Button)findViewById(R.id.btnCallLog);
ContactBtn=(Button)findViewById(R.id.btnContact);
SearchBtn=(Button)findViewById(R.id.btnSearch);
 
 
GalleryBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent=new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivity(myIntent);
}
});
 
CallLogBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent=new Intent();
myIntent.setAction(Intent.ACTION_CALL_BUTTON);
startActivity(myIntent);
}
});
 
ContactBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent=new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.Contacts.People.CONTENT_URI);
startActivity(myIntent);
}
});


SearchBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent=new Intent();
myIntent.setAction(Intent.ACTION_SEARCH);
startActivity(myIntent);
}
});
 
}


}

If you want to download the full project, you can download it by clicking on icon


No comments:

Post a Comment