What are Android Intents?

Intents
Intents are basically messages that are passed between components like activities, services, broadcast receivers and content providers. In a nutshell, intent is a messaging object you can use to request or an action or information from another app component. Intents can be used to start a new activity, pick a photo from you gallery, send an email message, launch a map, launch a webpage etc.


They are two types of intents in Android; they include implicit intents and explicit intents.
Explicit intents are used to invoke the activity class. In a nutshell, to link several activities together, you will have to use explicit intents. Apart from linking activities togetther, you can also send data or information from one activity to the other

Linking two Activities together (Link MainActivity with Main2Activity together)
Head on to your AndroidManifest.xml, your AndroidManifest.xml should look like this after creating the new Activity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.rexben.blogspotdemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Main2Activity"></activity>
    </application>

</manifest>

In your MainActivity.java
package com.android.rexben.blogspotdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //The method launches the new Activity
    //MainActivity.this is the Activity we are declaring the intent from
    //Main2Activity.class is the Activity that will be launched when this
    //method is trigerred
    public void nextActivity(View view){
        
        Intent newActivity = new Intent(MainActivity.this, Main2Activity.class);
        startActivity(newActivity);
    }
}

In your activity_main.xml

Passing data or information from one activity to another. I will show you how to pass texts (String) from the first Activity to the next Activity. Displaying the passed data in the next Activity - it's one thing to send a datum and it is another thing to display the datum.
Create another activity apart from your MainActivity and name it Main2Activity
In your MainActivity.java
package com.android.rexben.blogspotdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //The method launches the new Activity
    //MainActivity.this is the Activity we are declaring the intent from
    //Main2Activity.class is the Activity that will be launched when this
    //method is trigerred
    public void nextActivity(View view){
        EditText editText = (EditText) findViewById(R.id.best_color);
        String colorName = editText.getText().toString();
        Intent newActivity = new Intent(MainActivity.this, Main2Activity.class);
//Passing the entered text to the next activity
        newActivity.putExtra(Intent.EXTRA_TEXT, colorName);
        startActivity(newActivity);
    }
}

In your activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.rexben.sunshineapps.Main2Activity">

    <EditText
        android:id="@+id/best_color"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="casual"
        android:hint="Enter your best color"
        android:textSize="22sp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="nextActivity" />
</LinearLayout>


In your Main2Activity.java
package com.android.rexben.blogspotdemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        TextView textView = (TextView) findViewById(R.id.display_Color);
//Getting the data passed from the MainActivity
        Intent intent = getIntent();

        if (intent.hasExtra(Intent.EXTRA_TEXT)){
            String displayColor = intent.getStringExtra(Intent.EXTRA_TEXT);
            textView.setText("My best color is: " + displayColor);
        }
    }
}

In your activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context="com.android.rexben.blogspotdemo.Main2Activity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:id="@+id/display_Color"/>
</LinearLayout>

Implicit Intents are intents used to invoke the system's components. Unlike explicit intents where you know the Android components that will handle the task (Activity), you don't know the component because there might be one or more Application that can handle that task. For instance, when you want to allow users to send email messages from your App, you don't know the definite App that will handle the request. If the user has one email messaging App, it launches it instantly, if the users has more than one, the user is prompted to pick one.
I will be showing you how to use explicit intent to send text messages, email messages, launch a map, launch a web page
How to send a message
In your MainActivity.java
package com.android.rexben.blogspotdemo;

import android.os.Bundle;
import android.support.v4.app.ShareCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    public void sendMessage(View view){
        String type = "text/plain";
        String title = "Choose an App to complete this action";
        String message = "Visit www.rexandroid.blogspot.com to get started on" +
                "Android App developmenr. Thanks";

        ShareCompat.IntentBuilder.from(this)
                .setType(type).setChooserTitle(title).setText(message)
                .startChooser();
    }
}

In your activity_main.xml
<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"
    android:padding="16dp"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="cursive"
        android:textSize="22sp"
        android:text="Click on the button to send a message"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:onClick="sendMessage"
        android:text="Send a message"/>
</LinearLayout>

To Launch a web page
In your MainActivity.xml
package com.android.rexben.blogspotdemo;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private String myBlog = "http://www.rexandroid.blogspot.com";

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

    public void launchWebpage(View view) {
        Uri blogUri = Uri.parse(myBlog);
        Intent intent = new Intent(Intent.ACTION_VIEW, blogUri);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
}

In your activity_main.xml
<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"
    android:padding="16dp"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="cursive"
        android:textSize="22sp"
        android:text="Click on the button to launch a web page"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:onClick="launchWebpage"
        android:text="Launch the web page"/>
</LinearLayout>

To launch a map
In your AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.rexben.blogspotdemo">
    <!--Add this permission in order to access internet -->
<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Main2Activity"></activity>
    </application>

</manifest>

In your MainActivity.java
package com.android.rexben.blogspotdemo;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private String mapAddress = "1600 Amphitheatre Parkway, CA";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void launchMap(View view) {
        Uri.Builder builder = new Uri.Builder();
        builder.scheme("geo").path("0,0").query(mapAddress);
        Uri uri = builder.build();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(uri);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        } else {
            Toast.makeText(MainActivity.this, "You don't have the App to perform
this task",
                    Toast.LENGTH_LONG).show();
        }
    }
}

In your activity_main.xml
<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"
    android:padding="16dp"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="cursive"
        android:textSize="22sp"
        android:text="Click on the button to launch a map"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:onClick="launchMap"
        android:text="Launch a map"/>
</LinearLayout>

To dial a number
In your AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.rexben.blogspotdemo">
    <!--Add this permission in order to access internet -->
<uses-permission android:name="android.permission.CALL_PHONE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Main2Activity"></activity>
    </application>

</manifest>

In your MainActivity.java
package com.android.rexben.blogspotdemo;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;


public class MainActivity extends AppCompatActivity {

    private String number = "tel:2347032716134";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void dialNumber(View view) {
        Uri uriDial = Uri.parse(number);
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(uriDial);
        startActivity(intent);
    }
}

In your activity_main.xml
<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"
    android:padding="16dp"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="cursive"
        android:textSize="22sp"
        android:text="Click on the button to dial my number"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:onClick="dialNumber"
        android:text="Dial my number"/>
</LinearLayout>

No comments: