Tutorial details

Android beginner tutorial: Part 59 - Implicit Intents | App Code for Sale | Preview

Android beginner tutorial: Part 59 - Implicit Intents | Android Tutorial

How to use implicit Intents and intent filters

Overview PAGE TOP

Today we'll learn how to use implicit Intents and intent filters.

So far we've only used Explicit Intents and called Activities directly. Those always had a clear target - an Activity class to launch. Implicit Intents don't name a target.

An Explicit Intent is always delivered to its target, while Implicit ones need to pass a filter. Intent filters are elements of an application's manifest, which dictate which intents to receive and which ones to block.

An Intent Filter in the manifest file has fields that describe the action, data and category of the Activity. All these fields can be specified in the Implicit Intent and then compared with available filters. If the Intent's action, data and category match with the filters of an Activity, that Activity is launched.

Today we will update our application from the previous tutorial and create a new application, which launches the first one using an implicit intent.

Firstly, go to the AndroidManifest.xml of the first application. View it as plain XML file and you'll find this in the code:

       <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

We can add multiple filters for an activity. Let's create a new custom one, which also uses 2 fields - action and category. Set their values as shown:

    <activity
        android:name="com.kircode.codeforfood_test.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.kircode.codeforfood_test.LAUNCH_TEST" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

The "com.kircode.codeforfoodtest.LAUNCHTEST" String is what we will later give to an Implicit Intent to search for a suitable Activity to launch.

The manifest xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.kircode.codeforfood_test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.CAMERA"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.kircode.codeforfood_test.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.kircode.codeforfood_test.LAUNCH_TEST" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name="com.kircode.codeforfood_test.SecondActivity"></activity>
    </application>

</manifest>

Now create a new Application (File>New>Android Application Project). I called mine "CodeForFood Test Two". When you've created it, go to its layout xml and add a button:

<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" >

    <TextView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="32sp"
        android:text="Second Application"
        />

    <Button android:id="@+id/launchButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Launch CodeForFood"
        />

</LinearLayout>

In the java class of the new Activity, use this code:

package com.example.codeforfoodtest_two;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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 btn = (Button)findViewById(R.id.launchButton);
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent("com.kircode.codeforfood_test.LAUNCH_TEST");
startActivity(intent);
}
});
}

}

As you can see, the code to create an Implicit Intent is simple. All we do is specify the action as the parameter of the constructor.

If you also want to filter by data, specify the data in the second parameter, or using the setData() method.

If you want to filter by category too, use addCategory() method of the intent.

That's all for today!

We now have a second application, which launches the first application using an implicit intent.

Thanks for reading!

Reference PAGE TOP

http://kirill-poletaev.blogspot.it/2013/01/android-beginner-tutorial-part-59.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.