Search Projects

Tuesday, June 30, 2015

How to call built-in applications using intents in Android

Developing android apps specially the games required opengl for providing better graphics. We have seen how to get started with opengl es for android, and will continue the opengl tutorial  for android programming. Today's we are going to learn an important concept in this android development tutorial, how to call built-in applications using intents in Android. This give advantage to developer taking in-build apps and throw user to them if required. Calling built in applications using intents in Android is important as it give user more choices. For example in an app you want user make a call or open a document (pdf) let you use the in-build apps, take off the burden in developing those apps. You might have seen in previous Whatsapp, which let user call from within the app (now they have their own voip).

Android intent example

Lets get started with how to use intent in android. The following Try It Out demonstrates how to call some of the built-in applications commonly found on an Android device. 

Prerequisite


In this android intent example you need the Android Studio or Eclipse with ADT plugin and Android SDK tool. It is also necessary that you must know about some fundamentals of Java and XML as well as about the Android device. We also assume here that you know hello world program for developing android apps. This is a simple beginners tutorial, which hardly take you half an hour to understand, and doing yourself. I am doing this in Android Studio but you may choose Eclipse (remember google officially going to end support for ADT at the end of 2015).

Creating New Project


1. First step : Open Android Studio and Create a new project by selecting 'Start a new Android Studio Project'. Give it a name MyIntent and a package name in.ruks. You can have your own application package names. Click next, choose minimum SDK, in ours case it is API 14 : Android 4.0 (IceCreamSandwich), which accounts for close to 90% of activated android devices. Again click next, select the Blank Activity, Click next. Android will ask to name the activity, keep the default names, click on finish. Android Studio will create the the new Android Project with all the necessary files.

how to create new project in android studio for developing android apps

Set up Project Layout

We are going to use 6 built-in application in this android development tutorial. They are -
  1. Gallery
  2. Call Log
  3. Contact
  4. Dailler
  5. Browser
  6. Map

2. Second step : Now the time to set up the layout of our project. We are going to call 6 built-in applications using intents, hence we will place a button for each in activity_main.xml. You can drag and drop 6 button on the layout or simply create by coding the activity_main.xml file. Check out the code below and add it to activity_main.xml
<button android:id="@+id/button_contacts" android:layout_height="wrap_content" 
android:layout_width="fill_parent" android:text="@string/contacts">
    
<button android:id="@+id/button_gallery" android:layout_height="wrap_content" 
android:layout_width="fill_parent" android:text="@string/gallery">

<button android:id="@+id/button_Dail" android:layout_height="wrap_content" 
android:layout_width="fill_parent" android:text="@string/dial">

3. Third step :  It's bad idea to have the hard coded string in the project. So in this android intent example code, we are given name to each of the button within the strings.xml, which is under the values folder in Android Studio. Add the code below in it -

    MyIntent
    Hello world!
    Settings
    Contacts
    Gallery
    Call a number
    Open Call Logs
    Open Browser
    Open Map
    Exit


Now we have our layout design for this android intent example, we are going to code the java file to call built-in applications using intents.

Creating  event handler


4. Fourth step : In our MainActivity.java file, we are going to declare the button for each of the Button we have created in our layout file.

Button Gallery = (Button) findViewById(R.id.button_gallery);
Button CallLog = (Button) findViewById(R.id.button_calllog);
Button Contacts = (Button) findViewById(R.id.button_contacts);
Button Map = (Button) findViewById(R.id.button_map);
Button Dail = (Button) findViewById(R.id.button_Dail);
Button Broswer = (Button) findViewById(R.id.button_browser);
5. Fifth step : Our Buttons are ready to have some action, now its time to attach the event handler to them. One by one we are going to call setOnClickListener method on each of our Button and call built-in applications. Just place the following code in MainActivity.java
package in.ruks.myintent;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button Gallery = (Button) findViewById(R.id.button_gallery);
        Button CallLog = (Button) findViewById(R.id.button_calllog);
        Button Contacts = (Button) findViewById(R.id.button_contacts);
        Button Map = (Button) findViewById(R.id.button_map);
        Button Dail = (Button) findViewById(R.id.button_Dail);
        Button Broswer = (Button) findViewById(R.id.button_browser);

        Contacts.setOnClickListener(new Button.OnClickListener(){

            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Intent myIntent = new Intent();

                myIntent.setAction(Intent.ACTION_VIEW);
                myIntent.setData(android.provider.Contacts.People.CONTENT_URI);
                startActivity(myIntent);

            }
        });

        Gallery.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Intent myIntent = new Intent();

                myIntent.setAction(Intent.ACTION_VIEW);
                myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                startActivity(myIntent);

            }
        });
        Dail.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

    /*Intent myIntent = new Intent();

    myIntent.setAction(Intent.ACTION_VIEW);
    myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivity(myIntent);*/

                Intent i = new Intent(android.content.Intent.ACTION_DIAL,
                        Uri.parse("tel:+918147656011"));
                startActivity(i);
            }
        });
        CallLog.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Intent myIntent = new Intent();

                myIntent.setAction(Intent.ACTION_CALL_BUTTON);
                startActivity(myIntent);
            }
        });

        Broswer.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Intent i = new Intent(android.content.Intent.ACTION_VIEW,
                        Uri.parse("http://www.openglprojects.in"));
                startActivity(i);
            }
        });

        Map.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Intent myIntent = new Intent();

                myIntent.setAction(Intent.ACTION_VIEW);
                myIntent.setData(Uri.parse("geo:37.827500,-122.481670"));
                startActivity(myIntent);

            }
        });



    }

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

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        //return super.onOptionsItemSelected(item);
        if(item.getItemId()==R.id.action_exit){
            this.finish();
            return true;
        }
        return false;
    }


}

Declare Activity in  AndroidManifest


6. Final step : If you have basic knowledge of developing android apps then you must know that, every activity you have in your application must be declared in your AndroidManifest.xml file. So Know place the following code in AndroidManifest.xml file.

We finished with out coding session, time to see our work in action. Go to Run menu(alt+u) click Run App or directly run the  program with shift +F10.

Check out the demo video -


Final Thoughts


In this how to call built-in applications using intents tutorial, we have call only 6 in build apps, while you can call others as well. You can replace the mobile number with our own in android intent example activity replacing the tel:+918147656011. Same can be done with website url and map Lat Long values by replacing http://www.openglprojects.in and geo:37.827500,-122.481670 respectively in MainActivity.java file. 

Hope you liked this android development tutorial, feel free to ask anything via comments. We assume that this small step will help you in developing android apps.

Friday, June 19, 2015

OpenGL Projects Android App available on Google Play Store


Hello fellow reader of OpenGL Projects blog, we have just launched our own Android App.

Now the OpenGL Projects Android App available on Google Play Store for your Android smartphones and tablets.This is the first version of our app on Google Play Store.This app let you have all the feature of OpenGL projects in your pocket. You don't have to on browsing any web browser rather can easily view all the articles on our Android App. Let take the advantage of this app.

Download OpenGL Projects Android App available on Google Play Store

The OpenGL projects Android app is compatible with almost all the Android Devices including the Old Android OS 2.2 and new updated Android 5.1. We hope you will download the app and take the full advantage with you.

Please not forget to rate our App and give us feedback to make it better.