Technical Knowledge

technical knowledge is that type of blogger in which you can find all the soluctions of your problems which you cannot solve you can see the video given on this blog and solve your problem by yourself.

Like us on Facebook

Technical Knowledge

Sunday, March 29, 2015

Using implicit intent: Android tutorial

Program description:

This is a demo program on how to start an activity by using implicit intent.

This program has 2 activities, first activity will start second activity by using implicit intent.
First activity has 2 buttons, on clicking first button we use implicit intent to trigger second activity. This implicit intent uses 1 action and 2 categories.
On clicking second button, it will start same second activity by using explicit intent.

Once second activity is triggered, it will display the action, and categories which has triggered that activity by using getIntent() method.

Intents are 2 types: Explicit intent, and implicit intent.

1.Explicit intent : An intent which contains target component name in it. We generally use explicit intent to communicate between components of same application.
2.Implicit intent : An intent which doesn't contain target component name. Since there is no target component name, we have to provide some clues to android to trigger a component. Those clues will be in the form of action, categories, and data. We generally use implicit intent to communicate with other application components, as we don't know their component names. 

This example will demonstrate both implicit and explicit intent.

difference between explicit and implicit intent


First activity

package com.techpalle.b15_test3;

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

public class MainActivity extends Activity {
Button b1, b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent();
in.setAction("com.techpalle.action.MYACTION");
in.addCategory("com.techpalle.category.MYCATEGORY");
in.addCategory("com.techpalle.category.MYCATEGORY2");
startActivity(in);
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(getApplicationContext(),
SecondScreen.class);
startActivity(in);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

xml file for first activity 
File name : activity_main.xml
  xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

Second activity

package com.techpalle.b15_test3;

import java.util.Set;

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

public class SecondScreen extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("hey..");
setContentView(tv);
Intent in = getIntent();
if(in != null){
String action = in.getAction();
Set cat = in.getCategories();
Toast.makeText(getApplicationContext(), "action.."+action,
0).show();
Toast.makeText(getApplicationContext(), "cat.."+cat,
0).show();
}
// TODO Auto-generated method stub
}

}

Manifest file 
File name : AndroidManifest.xml

  
package="com.techpalle.b15_test3"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="17" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.techpalle.b15_test3.MainActivity"
android:label="@string/app_name" >
















Download complete code : Click to download

Android interview questions on Intents:
What is the difference between implicit intent and explicit intent?
What are the components of an intent?
What is putExtra in intent?
What is the difference between intent, pending intent, and sticky intent?
Is it possible to have extras in intent-filter?
What is the difference between intent and intent-filter?
How intent and intent-filter works in android? What is intent test?



Tags: android, implicit, intent, explicit, getIntent

No comments:

Post a Comment