Tuesday, April 1, 2014

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 see 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:


<LinearLayoutxmlns: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:

packagesel.impli; //your package name
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.view.View;

publicclassMainActivity extendsActivity {
@Override
protectedvoidonCreate(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
  publicvoidcall(View v)
  {
   Intent i=null;
   //get id from the view object and
   //perform action according to button id
   switch(v.getId())
   {
     caseR.id.internet:
     //if id is internetthan open net
     //use action view because we want to view the given websitepage
     i=newIntent(Intent.ACTION_VIEW, Uri.parse("http://www.innosen.blogspot.com"));
     startActivity(i);break;
     caseR.id.fb:
     //if id is fbthan open facebook
     //use action view because we want to view the given websitepage
     i=newIntent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com"));
     startActivity(i);break;
    caseR.id.phone:
    //if id is phone than open phone dialer
    //use action dial because we want to open phone dialer
    i=newIntent(Intent.ACTION_DIAL, Uri.parse("tel:8527801400"));
    startActivity(i);break;
    caseR.id.bigb:
    //if id is bigbthan call this no. +918527801400
    //use action call becausewe want to call on the given no.
    i=newIntent(Intent.ACTION_CALL, Uri.parse("tel:+918527801400"));
    startActivity(i);break;
    caseR.id.cam:
    //if id is camthan use camera driver path to open camera
    i=newIntent("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:

<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns: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-permissionandroid:name="android.permission.INTERNET"/>
<uses-permissionandroid:name="android.permission.CAMERA"/>
<uses-permissionandroid: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>
   <actionandroid:name="android.intent.action.MAIN"/>
   <categoryandroid: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. Thanks... :)

Related Posts by Categories

0 comments:

Post a Comment