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>