Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the incoming message using service in background in android?

Tags:

android

sms

I am developing an android application in that i want to read the incoming message without knowing the user.I want to always run the incoming message checker in background.If a new message is received means i want to read the content of the message and that message contains some words(password) means i want to activate the application

Please explain me how to do that with sample code because i am new to android

like image 980
Krishna Avatar asked Feb 11 '11 20:02

Krishna


People also ask

How do I read incoming messages?

Head back to Google Assistant or say, “OK/Hey, Google,” again, and then repeat the, “Read my text messages,” instruction. Google Assistant will start at the beginning, and read your text message notifications aloud, as well as notifications about messages from other sources, like WhatsApp.

How Services is working in the background?

For Android Developers, a Service is a component that runs on the background to perform long-running tasks. A Background Service is a service that runs only when the app is running so it'll get terminated when the app is terminated. A Foreground Service is a service that stays alive even when the app is terminated.

How do I run background services on Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view, when user click on text view, it will start service and stop service.


1 Answers

Take a look at BroadCastReceivers you must implement and register a Reciever for android.provider.Telephony.SMS_RECEIVED

Here is a code snippet that lets you read messages as they arrive.

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
{
    public void onReceive(Context context, Intent intent)
    {
        Bundle myBundle = intent.getExtras();
        SmsMessage [] messages = null;
        String strMessage = "";

        if (myBundle != null)
        {
            Object [] pdus = (Object[]) myBundle.get("pdus");
            messages = new SmsMessage[pdus.length];

            for (int i = 0; i < messages.length; i++)
            {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                strMessage += "SMS From: " + messages[i].getOriginatingAddress();
                strMessage += " : ";
                strMessage += messages[i].getMessageBody();
                strMessage += "\n";
            }

            Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();
        }
    }
}

And here what you have to add to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.RECEIVE_SMS" />

<receiver android:name=".SMSReceiver">
    <intent-filter> 
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
    </intent-filter> 
</receiver>
like image 59
Lucas S. Avatar answered Oct 21 '22 21:10

Lucas S.