Pages

Sunday, 29 September 2013

Access call, dialer screen, camera and web pages in Android

In this application we will learn how to access phone services using our activity. Implicit Intent is used to access all services which come with android phone. First  we will learn what is Intent and than what is Implicit Intent?

Intent: Intent is an android service that gives us the facilities of accessing one activity from the other activity. Basically, There are two types of Intent:-

1) Implicit Intent

2) Explicit Intent

Implicit Intent:- Accessing system define activities from the user define activity like phone call, camera, browser etc.

Here we are giving an example of Implicit Intent. Create new project and drop buttons in linear layout and keep all images in drawable folder. The code of android XML file is given below:

Access call, dialer screen, camera and web pages in Android

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal"
  android:background="#363">
<Button
  android:id="@+id/internet"
  android:layout_width="56dp"
  android:layout_height="62dp"
  android:layout_marginLeft="6dp"
  android:background="@drawable/internet"
  android:onClick="call" />
<Button
  android:id="@+id/fb"
  android:layout_width="56dp"
  android:layout_height="62dp"
  android:layout_marginLeft="6dp"
  android:background="@drawable/facebook"
  android:onClick="call"
/>
<Button
  android:id="@+id/bigb"
  android:layout_width="56dp"
  android:layout_height="62dp"
  android:layout_marginLeft="6dp"
  android:background="@drawable/bigb2"
  android:onClick="call" />
<Button
  android:id="@+id/cam"
  android:layout_width="56dp"
  android:layout_height="62dp"
  android:layout_marginLeft="6dp"
  android:background="@drawable/cam"
  android:onClick="call" />
<Button
  android:id="@+id/phone"
  android:layout_width="56dp"
  android:layout_height="62dp"
  android:layout_marginLeft="6dp"
  android:background="@drawable/phone"
  android:onClick="call" />
</LinearLayout>

Now open your Java file and initialize all objects. Use the following tricks in implicit Intent:

Intent obj=new Intent(What to do, with whom to do);

URI (uniform resource identifier) is used to identify universal values. The code of android Java file is given below with explanation:-


package sel.impli; //your package name
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  }
  //This method is called when we click on any button
  //and it is declare in main XML file in button tag
  //we have to pass View object in this method
  //view object has the id of button which is clicked
  public void call(View v)
  {
   Intent i=null;
   //get id from the view object and
   //perform action according to button id
   switch(v.getId())
   {
     case R.id.internet:
     //if id is internet than open net
     //use action view because we want to view the given website page
     i=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.innosen.blogspot.com"));
     startActivity(i);break;
     case R.id.fb:
     //if id is fb than open facebook
     //use action view because we want to view the given website page
     i=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com"));
     startActivity(i);break;
    case R.id.phone:
    //if id is phone than open phone dialer
    //use action dial because we want to open phone dialer
    i=new Intent(Intent.ACTION_DIAL, Uri.parse("tel:8527801400"));
    startActivity(i);break;
    case R.id.bigb:
    //if id is bigb than call this no. +918527801400
    //use action call because we want to call on the given no.
    i=new Intent(Intent.ACTION_CALL, Uri.parse("tel:+918527801400"));
    startActivity(i);break;
    case R.id.cam:
    //if id is cam than use camera driver path to open camera
    i=new Intent("android.media.action.IMAGE_CAPTURE");
    startActivity(i);break;
   }
  }
}

All work finished but we are using other application services so we have to take permission to use services. So, open your AndroidManifest.xml file and add all required permission like below code:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="sel.impli"
  android:versionCode="1"
  android:versionName="1.0" >
<uses-sdk
  android:minSdkVersion="10"
  android:targetSdkVersion="10" />
 
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
 
<application
  android:allowBackup="true"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/AppTheme" >
<activity
  android:name="sel.impli.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>

Now run your project and test application. If you have any doubt please comment. Share and help other. 

Related Tutorials:- 

Open Second Activity using Intent

Make any column invisible in Table layout

Create a Dialog box

Send SMS without any Report

Create List and perform Actions on it

11 comments:

  1. sir can you tell how to show php web content in android ? please

    ReplyDelete
    Replies
    1. Use webview to show html or php contents.

      Delete
    2. Hello brother can tell me about when we click on button just link to any soft?
      Ex. click button facebook so it link to auto no link to web.

      Delete
    3. Thank You Sooooooo Much Bro

      Delete
  2. thank sir ,what is class Intent?

    ReplyDelete
    Replies
    1. Already described in this post.

      Delete
  3. Anonymous11:38 am

    This comment has been removed by a blog administrator.

    ReplyDelete
  4. sir i want to create my own dialer app..
    means i want to create my own customised screen ...
    can you tell me what to do????

    ReplyDelete
  5. Hi, your content is really informative and helpful for novices like me.

    Great Job.

    ReplyDelete
  6. I like this source code

    ReplyDelete

Back to Top