Intents
An Intent is a messaging object. We can use to request an action from an other application component. Although intents facilitate communication between components in several ways.
Types of Intents:
There are two types of intents:
- Explicit intents specify the component to start by name (the fully-qualified class name). You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start. For example, start a new activity.
- Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location web url.
IntentExample.java
package com.example.crudapps;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
public class IntentExample extends Activity implements OnClickListener {
Button btn_explicit,btn_implicit,btn_weburl;
Intent mintent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent_example);
btn_explicit = (Button)findViewById(R.id.btn_explicit);
btn_explicit.setOnClickListener(this);
btn_implicit = (Button)findViewById(R.id.btn_implicit);
btn_implicit.setOnClickListener(this);
btn_weburl = (Button)findViewById(R.id.btn_weburl);
btn_weburl.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btn_explicit:
mintent = new Intent(IntentExample.this,NextActivity.class);
startActivity(mintent);
break;
case R.id.btn_implicit:
mintent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://chandandroid.blogspot.in/"));
startActivity(mintent);
break;
case R.id.btn_weburl:
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.loadUrl("http://chandandroid.blogspot.in/");
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.intent_example, menu);
return true;
}
}
activity_intent_example.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"
android:background="@drawable/bg_background"
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="com.example.crudapps.IntentExample" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<Button
android:id="@+id/btn_explicit"
android:layout_width="458dp"
android:layout_height="wrap_content"
android:text="Explicit" />
<Button
android:id="@+id/btn_implicit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Implicit" />
<Button
android:id="@+id/btn_weburl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Web URL" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout1"
android:layout_below="@+id/linearLayout1"
android:layout_marginLeft="96dp"
android:layout_marginTop="53dp"
android:orientation="vertical" >
</LinearLayout>
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/linearLayout2" />
</RelativeLayout>
Screen:-
Implicit Intent
No comments:
Post a Comment