036 Java Program to Demonstrate Implicit Intent
Posted by Superadmin on March 06 2016 11:34:11
Java Android Program to Demonstrate Implicit Intent
This Java Android Program demonstrates Implicit Intent.
Here is source code of the Program to demonstrate Implicit Intent. The program is successfully compiled and run on a Windows system. The program output is also shown below.
Intents are used as a message-passing mechanism that works both within your application and between applications. You can use Intents to do the following:
1. Explicitly start a particular Service or Activity using its class name
2. Start an Activity or Service to perform an action with (or on) a particular piece of data
3. Broadcast that an event has occurred
You can use Intents to support interaction among any of the application components installed on an Android device, no matter which application they’re a part of. This turns your device from a platform containing a collection of independent components into a single, interconnected system.
One of the most common uses for Intents is to start new Activities, either explicitly (by specifying the class to load) or implicitly (by requesting that an action be performed on a piece of data). In the latter case the action does not need to be performed by an Activity within the calling application.
Intents
1. Implicit
2. Explicit
An implicit Intent is a mechanism that lets anonymous application components service action requests. That means you can ask the system to start an Activity to perform an action without knowing which application, or Activity, will be started.
For example, to let users make calls from your application, you could implement a new dialer, or you could use an implicit Intent that requests the action (dialing) be performed on a phone number (represented as a URI).
if (some condition) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(“tel:555-2368”));
startActivity(intent);
}
Android resolves this Intent and starts an Activity that provides the dial action on a telephone number
— in this case, typically the Phone Dialer.
When you use an implicit Intent to start an Activity, Android will — at run time — resolve it into the Activity class best suited to performing the required action on the type of data specified. This means you can create projects that use functionality from other applications without knowing exactly which application you’re borrowing functionality from ahead of time.
In circumstances where multiple Activities can potentially perform a given action, the user is presented with a choice.
Incorporating or using other functionality from other Activities is extremely useful but we are not sure that the particular functionality will resolve or not , therefore it is actually a good practice to determine your call would resolve or not before calling the startActivity() method thus you can first query the Package Manager to determine which, if any, Activity will be launched to service a specific Intent by calling resolveActivity on your Intent object.
MainActivity
package com.example.implicit_intent;
import android.net.Uri;
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.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Spinner spinner_object;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner_object = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.intents, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_object.setAdapter(adapter);
Button but = (Button) findViewById(R.id.button1);
but.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = spinner_object.getSelectedItemPosition();
Intent intent = null;
switch (position) {
case 0:
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
break;
case 1:
// dials the call directly to the number specified
intent = new Intent(Intent.ACTION_CALL,
Uri.parse("tel:(+0129)2462799"));
break;
case 2:
// opens up the to - dial but does not dials the number
intent = new Intent(Intent.ACTION_DIAL,
Uri.parse("tel:(+0129)2420479"));
break;
case 3:
/*
* opens up the specified location in the google map by its
* longitude and latitude
*/
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:28.632323175362604,77.22595474023433?z=19"));
break;
case 4:
// to search for india gate on google map
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=india gate"));
break;
case 5:
// opens up the camera to capture an image
intent = new Intent("android.media.action.IMAGE_CAPTURE");
break;
case 6:
// opens up contacts menu of your phone
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("content://contacts/people/"));
break;
case 7:
// edits the first contact on your contact list
intent = new Intent(Intent.ACTION_EDIT,
Uri.parse("content://contacts/people/1"));
break;
}
if (intent != null) {
startActivity(intent);
}
}
@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;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 0) {
String result = data.toURI();
Toast.makeText(this, result, Toast.LENGTH_LONG);
}
}
}
Xml
Main
<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: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" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner1"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="26dp"
android:text="Button" />
</RelativeLayout>
Create the following intents.xml in your /res/value
Intents.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="intents">
<item>Open Browser</item>
<item>Dial a Call </item>
<item>Dial</item>
<item>Show Map</item>
<item>Search on Map</item>
<item>Take picture</item>
<item>Show contacts</item>
<item>Edit first contact</item>
</string-array>
</resources>
Here is the Android Manifest , do have a look at it and see that we have added some permissions here in order to have permissions from the user to use for eg internet, camera etc.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.implicit_intent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.implicit_intent.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>