Tutorial details

Android beginner tutorial: Part 57 - Standard Intents | App Code for Sale | Preview

Android beginner tutorial: Part 57 - Standard Intents | Android Tutorial

Invoking standard Activites using Intents

Overview PAGE TOP

Today we will learn about invoking standard Activites using Intents.

As I mentioned in the previous tutorials, it is possible to use Intents to launch Activities not only within the main application, but also call out other application Activities. There are some standard Android Activities that we can launch, which invovle standard actions like dialing, using the camera, launching the music player, recording sound etc.

Those are called out using the same Intent objects we used before, except that a certain String value has to be passed to the constructor of the Intent object. Moreover, some actions require user permissions, which the user is asked to agree to when the application is installed. These are declared in the AndroidManifest XML file.

Go to the manifest file now using Eclipse IDE, switch to the Permissions tab and Add 2 "Uses Permission" lines to the list. Select each of them and set their Names to android.permission.CALL_PHONE and android.permission.CAMERA. You can find those in the dropdown menu of the input field.

You can add as many permissions as you want, but only pick those that your application is going to use. Also think carefully about what you're requesting to permit - some users are discouraged by certain permission requests when installing applications. Also, some of the things you're requesting might not be available for everyone. For example, some phones might simply not have the camera that you're asking for.

Now go to activity_main.xml file. Add 3 buttons labeled "Call", "Camera" and "Record sound":

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <Button android:id="@+id/callButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Call"
        />

    <Button android:id="@+id/cameraButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Camera"
        />

    <Button android:id="@+id/recordButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Record sound"
        />

</LinearLayout>

Here is the MainActivity.java class code for the application:

package com.kircode.codeforfood_test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.Media;
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);

final Button callBtn = (Button)findViewById(R.id.callButton);
callBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
}
});

final Button cameraBtn = (Button)findViewById(R.id.cameraButton);
cameraBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
startActivity(intent);
}
});

final Button recordBtn = (Button)findViewById(R.id.recordButton);
recordBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(Media.RECORD_SOUND_ACTION);
startActivity(intent);
}
});
}

}

It's that simple. All you need to do is start an Intent after passing a String value to the constructor of the Intent.

That's all for today.

Thanks for reading!

Reference PAGE TOP

http://kirill-poletaev.blogspot.it/2013/01/android-beginner-tutorial-part-57.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.