Android Intent Filter Tutorial

Android Intent Filter Tutorial

Howdy viewers, welcome to Android Intent Filter Tutorial where you’ll discover the steps to develop the android intent filter application. This tutorial is very simple and easy to understand, primarily focuses on Beginner’s level. By the end of this online free tutorial, you will understand and grab the idea to develop an Android application using Intent Filter. Again, if you’re developing an Advanced Android application using Intent Filter, then I would suggest you to go with this basic tutorial. If you have any doubts and correct me, please comment below as I would love to hear from you.

Problem: Create an Android application using Intent Filter where incoming SMS can be displayed within it with Sender’s information and Message body.

Let’s get started:

Well, first thing you’re going to design the user interface, activity_main.xml. The design is very simple with no hard working done, that’s pretty much it for this tutorial.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.raktim.fancysms.MainActivity">

<TextView
    android:id="@+id/tv"
    android:text="Intercepting SMS example"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
    <ScrollView
        android:layout_below="@+id/tv"
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/theMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>
</RelativeLayout>
=============
Explanation:
=============
1. We have two TextView's, one for displaying the header and the other for diplaying the Sender and Message Body within the application. To display the heading we added tv as its id and to display the Sender and Message Body within the application we added theMessage as its id. We have wrapped theMessage TextView id within ScrollView that will give scrolling feature when messages goes beyond the screen.

Next step, go ahead and work with MainActivity.java file.

package com.raktim.fancysms;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
static TextView txtMsg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtMsg=(TextView)findViewById(R.id.theMessage);
    }
}
============
Explanation:
============
1. The code is very simple and I assume you understand it without any explanation.

Our launcher java (MainActivity.java) file is done, next we need a java file extended to Broadcast Receiver. We call the java file as SmsReceiver.java.

package com.raktim.fancysms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle=intent.getExtras();
        Object messages[]= (Object[]) bundle.get("pdus");
        SmsMessage smsMessage[]=new SmsMessage[messages.length];
        String msg="";
        int smsPieces=messages.length;
        for(int n=0;n<smsPieces;n++){
            smsMessage[n]=SmsMessage.createFromPdu((byte[])messages[n]);
            msg +="\n"+(n+1)+"-of-"+smsPieces+"\n"
                    +"Sender:\t" +smsMessage[n].getOriginatingAddress()+"\n"
                    + "Body: \n " + smsMessage[n].getMessageBody();
        }
        Toast.makeText(context, "FANCY >>> RECEIVED SMS"+smsMessage[0].getMessageBody(), Toast.LENGTH_LONG).show();
        MainActivity.txtMsg.setText(msg);
    }
}
==============
Explanation:
==============
1. We need to extend it to BroadcastReceiver because we are dealing with incoming SMS.
2. Next, within onReceive method (automatically created when BroadcastReceiver is called), we write the entire logical code.
3. We use Bundle to hold the implicit intent SMS in memory temporary. Bundle bundle=intent.getExtras();
4. We declare objects as an array to hold all the incoming SMS with Protocol Data Unit support.Object messages[]= (Object[]) bundle.get("pdus");
5. We declared SmsMessage object to hold the array of messages and its length to notify on receive. SmsMessage smsMessage[]=new SmsMessage[messages.length];
6. Within for loop we fetched the incoming messages from the Protocol Data Unit and list the variable's to show the output in the screen.

Wait, the most important part we missed. You need to seek permission to read receive SMS from the user and also statically register the BroadcastReceiver and intent filter. To do this, modify AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.raktim.fancysms">
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <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>
<receiver android:name=".SmsReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
</receiver>
    </application>

</manifest>
=============
Explanation:
=============
1. Ask permission from the user to RECEIVE_SMS. The code is <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
2. Now register the receiver statically. This can be achieved by the code <receiver android:name=".SmsReceiver" android:enabled="true">
 <intent-filter>
 <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
 </intent-filter>
</receiver>
3. The <intent-filter> is wrapped within the receiver because when SMS is received, tell the device to take action. The action package is android.provider.Telephony.SMS_RECEIVED

NOTE:

You need to run the android application on real device. Install it on one device and try to send SMS from another device. Give yourself try. However, if you want to run on emulator, you have to open two emulators in parallel and follow the concept as in real device. Standard SMS rates apply.

Conclusion:

That’s it, I assume that you by now got the brief idea about the Android Intent Filter Tutorial. If you have any queries, drop comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *