1

Hello im trying to make a app that will Always Listen out for a SMS containing text and then if it does just give a visual indicator i would like the listener to run in the back ground all the time this is all the code i have

package me.andrew.findmyphone;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

/**
 * Created by Andrew on 15/12/2015.
 */
public class SMSReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context,Intent intent){
        Bundle bundle = intent.getExtras();

        if(bundle != null){
            Object[] pdus = (Object[]) bundle.get("pdu");

            for(int i = 0; i < pdus.length; i++){
                SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdus[i]);
                String message = sms.getMessageBody();
                String status = "";
                if(message.equalsIgnoreCase("!andrew")){
                    status = "true";
                }else{
                    status = "false";
                }
                Toast.makeText(context,status+" the msg is a trigger msg",Toast.LENGTH_LONG).show();
            }
        }
    }

}

Currently What I was trying to do was if the msg contained "!andrew" to display a notification this class is the SMSReceiver class in the Main ActivityClass it is just the default code and the android manifest looks like this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="me.andrew.findmyphone" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
        </activity>
        <receiver android:name=".SMSReceiver">
            <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
        </receiver>
        <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    </application>
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
</manifest>
Andrew Aubury
  • 113
  • 12
  • If you create an `` within your `` with an `` for `"android.provider.Telephony.SMS_RECEIVED"`, the Receiver will be started whenever an incoming SMS is received. You don't need anything running constantly in the background. You can also remove both of the `` elements inside the `` tags. You just need the one outside. – Mike M. Dec 16 '15 at 02:03
  • The question in [this post](http://stackoverflow.com/questions/14452808/sending-and-receiving-sms-and-mms-in-android-pre-kit-kat-android-4-4) has an example in the manifest. – Mike M. Dec 16 '15 at 02:08
  • @MikeM.Thanks will try this now also is there any way to make the reciver launch on device start? – Andrew Aubury Dec 16 '15 at 11:38
  • I don't know why you'd want to do that. Your Receiver is meant to handle incoming SMS, and it will be started whenever one is received by the system, provided that you've implemented everything correctly, and that the user has run the app at least once after installation. Running that Receiver at boot up is effectively going to do nothing, because the Intent won't have an SMS attached to it. If you do want to receive a broadcast upon booting, you'll need a Receiver filtering for the `BOOT_COMPLETED` action, and you'll need the `RECEIVE_BOOT_COMPLETED` permission. – Mike M. Dec 16 '15 at 11:45
  • @MikeM. ok Thanks Mike i was under then impression the app must be running to handle them next issue is when it gets the SMS it crashes – Andrew Aubury Dec 16 '15 at 11:51
  • Well, you need to look at the stacktrace in your logcat to determine what the error is, and where it's happening. [This post](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) might be of some help. – Mike M. Dec 16 '15 at 11:53
  • @MikeM.Thanks one last thing i think you may be able to help me with would be when it get the text to set Device Volume to max then Ring for 1 min – Andrew Aubury Dec 16 '15 at 11:55
  • That's really a completely different question, and the powers that be don't like it when you ask more than one per post. That said, there are many examples of how to adjust a device's volume already on the site. If you can't find any with the site's search tool, try Google with `site:stackoverflow.com` prepended to your search. As you can imagine, Google's a little better at searching than most sites are on their own. – Mike M. Dec 16 '15 at 12:00

0 Answers0