Android Intents
==> Dictionary meaning of Intent is Intention/ purpose that means intention to do action.
==> Intent is nothing but a message that is passed between Activities, Content Providers, broadcast Receivers, services etc.
Ex: if we want to invoke a new Activity we need to pass new Activity ,
if we want to start other application from your Activity , then we need to fire an Intent. That means we are telling te Android system to make something happen.
Usage of Intents in Android
1) to start service
2) to launch Activity
3) to display list of contacts
4) to Broadcast a message
5) to dial phone call etc.
Type of Intents in Android
mainly there are 2 types.
1) Implicit intent
2) Explicit intent
Implicit intent
Implicit intent does not specify the component, in such case Intents provides information of available components that is to be invoked which are provided by system.
Ex:
1) Intent i =new Intent(Intent.Action_View);
i.setData(Uri.parse("http://www.tutorialforandroidbypatel.blogspot.in"));
startActivity(i);
2) //For making a call programatically
Intent i=new Intent(Intent.Action_Call);
i.setData(Uri.parse("tel:9999999999"));
startActivity(i);
Explicit Intent
Explicit intent specifies the component, in such case intent provides the external Activity name which is to be invoked. Also we can pass information from one Activity to another Activity.
Ex: startActivity(new Intentt(getApplicationcontext(), NewActivity.class));
Android startActivityForResult
Using this we can send information from one Activity to another Activity vice versa. This requires the Result from Activity2 to Activity1.
in such case Automatically onActivityResult of Activity1 will invoke and it will receive the result which Activity2 returns.
Ex:
Step 1) Activity1.java
// write the below code in onClick event of get Button.
public void get(View v)
{
Intent i=new Intent(getApplicationContext(), Activity2.class);
startActivityForResult(i,2);
}
protected void onActivityResult(int requestcode, int resultcode, Intent data)
{
super.onActivityResult(requestcode,resultcode,data);
if(requestcode==2)
{
string msgfromActivity2 = data.getStringExtra("MESSAGE");
tv1.setText(msgfromActivity2);
}
}
Step 2) Activity2.java
// write the below code in onClick event of set button
public void set(View v)
{
Intent i=new Intent();
i.putExtras("MESSAGE" , tv2.getText().toString());
setResult(2,i);
finish();
}
Example for different Intents use in Android?
Step 1> Write below code in activity_main.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" >
<EditText android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/txtCall"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnCall"
android:text="Dial"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Navigate to Another app"
android:id="@+id/btnAppNavi"
android:layout_marginTop="20dp"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Navigate to Activity"
android:id="@+id/btnActvitiyNavi"
android:layout_marginTop="20dp"/>
</LinearLayout>
Step 2> Write the below in MainActivity.java file
package com.example.intentsexample;
import java.util.jar.Pack200.Packer;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
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 MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnDial=(Button)findViewById(R.id.btnCall);
Button btnActivityNavigate=(Button)findViewById(R.id.btnActvitiyNavi);
Button btnNavigateApp=(Button)findViewById(R.id.btnAppNavi);
btnDial.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
EditText txt=(EditText)findViewById(R.id.txtCall);
Intent i=new Intent();
i.setAction(Intent.ACTION_DIAL);
Uri.Builder builder=new Uri.Builder();
builder.path(txt.getText().toString());
builder.scheme("tel");
i.setData(builder.build());
startActivity(i);
}
});
btnActivityNavigate.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
Intent i=new Intent();
i.setComponent(new ComponentName(getApplicationContext(), Activity2.class));
startActivity(i);
}
});
btnNavigateApp.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
PackageManager pm=getPackageManager();
Intent i=pm.getLaunchIntentForPackage("");
startActivity(i);
}
});
}
}
Step 3> Create a new activity2.xml file and write below.
Step 1) Activity1.java
// write the below code in onClick event of get Button.
public void get(View v)
{
Intent i=new Intent(getApplicationContext(), Activity2.class);
startActivityForResult(i,2);
}
protected void onActivityResult(int requestcode, int resultcode, Intent data)
{
super.onActivityResult(requestcode,resultcode,data);
if(requestcode==2)
{
string msgfromActivity2 = data.getStringExtra("MESSAGE");
tv1.setText(msgfromActivity2);
}
}
Step 2) Activity2.java
// write the below code in onClick event of set button
public void set(View v)
{
Intent i=new Intent();
i.putExtras("MESSAGE" , tv2.getText().toString());
setResult(2,i);
finish();
}
Example for different Intents use in Android?
Step 1> Write below code in activity_main.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" >
<EditText android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/txtCall"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnCall"
android:text="Dial"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Navigate to Another app"
android:id="@+id/btnAppNavi"
android:layout_marginTop="20dp"/>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Navigate to Activity"
android:id="@+id/btnActvitiyNavi"
android:layout_marginTop="20dp"/>
</LinearLayout>
Step 2> Write the below in MainActivity.java file
package com.example.intentsexample;
import java.util.jar.Pack200.Packer;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
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 MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnDial=(Button)findViewById(R.id.btnCall);
Button btnActivityNavigate=(Button)findViewById(R.id.btnActvitiyNavi);
Button btnNavigateApp=(Button)findViewById(R.id.btnAppNavi);
btnDial.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
EditText txt=(EditText)findViewById(R.id.txtCall);
Intent i=new Intent();
i.setAction(Intent.ACTION_DIAL);
Uri.Builder builder=new Uri.Builder();
builder.path(txt.getText().toString());
builder.scheme("tel");
i.setData(builder.build());
startActivity(i);
}
});
btnActivityNavigate.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
Intent i=new Intent();
i.setComponent(new ComponentName(getApplicationContext(), Activity2.class));
startActivity(i);
}
});
btnNavigateApp.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
PackageManager pm=getPackageManager();
Intent i=pm.getLaunchIntentForPackage("");
startActivity(i);
}
});
}
}
Step 3> Create a new activity2.xml file and write below.
<?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:background="#FFFFFF">
<TextView android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="Welcome to new Activity"
android:textSize="30dp"
android:gravity="center"
android:textColor="#000000"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#FFFFFF">
<TextView android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="Welcome to new Activity"
android:textSize="30dp"
android:gravity="center"
android:textColor="#000000"/>
</RelativeLayout>
Step 4> Create a new Actvity2.java and write the below code.
package com.example.intentsexample;
import android.app.Activity;
import android.os.Bundle;
public class Activity2 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
}
}
package com.example.intentsexample;
import android.app.Activity;
import android.os.Bundle;
public class Activity2 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
}
}
Step 5> Write the below code in Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intentsexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity android:name=".Activity2" android:screenOrientation="portrait"></activity>
<activity
android:name="com.example.intentsexample.MainActivity"
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>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intentsexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity android:name=".Activity2" android:screenOrientation="portrait"></activity>
<activity
android:name="com.example.intentsexample.MainActivity"
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>





No comments:
Post a Comment