-2

I'm making an app which should be launched when the Phone receives an sms with a specific content (like a password). I've tried a lot of code but it still doesn't work. Can anyone please help me out?

this is my code: SmsReceiver.java

package com.example.william.better_gps;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "message", Toast.LENGTH_LONG).show();

}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.william.better_gps" >
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />

 <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>
    </activity>
    <receiver android:name=".SmsReceiver"
        android:enabled="true">
        <intent-filter android:priority="999">
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>

        </intent-filter>
    </receiver>



</application>
</manifest>

Ooh and is there any way i can simulate a sms so i don't have to ask someone to text to me if i want to check wether it's working

2 Answers2

1

You are setting the app to run as a launcher app, which will only happen when you click on the icon to run. You need to specify the intent filter for launching the app when you receive SMS. Then in the code you can set up a broadcast receiver in Main activity to listen for subsequent texts.

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme.NoActionBar" >
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
  • But if i do so, i can't run it anymore on my device because i get an error "MainActivity is not assignable to android.app.Activity". I'm pretty new in android programming so maybe it has another reason i can't run it – Willem Govaerts Nov 16 '15 at 20:48
1

Do not use compileSdkVersion,targetSdkVersion, buildToolsVersion 23 or greater than that.Because in the API level 23 action SMS_RECEIVED in broadcast receiver gives permission denied
Use 21 or lower than that.below configuration works for me

compileSdkVersion 21
buildToolsVersion "21.1.2"
targetSdkVersion 21

Ashraful Islam
  • 12,470
  • 3
  • 32
  • 53
  • if you build it with 23 no device will call the SMS_RECEIVED broadcast receiver build with 21 or lower it will work for all device, i have tested with my nexus 5 which has android marshmallow (API 23) and works – Ashraful Islam Nov 17 '15 at 03:32
  • Your app's target API level is 23, that is android M (6.0). In android M there are huge changes related to user-permissions.check the answer http://stackoverflow.com/questions/33036523/broadcast-receivers-not-working-in-android-6-0-marshmallow – Ashraful Islam Nov 17 '15 at 03:43
  • There is one thing, I am giving my answer to your question, so if you apply it and it's not working then you should comment about it and ask me to proof – Ashraful Islam Nov 17 '15 at 03:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95374/discussion-between-ashraful-islam-and-mike-m). – Ashraful Islam Nov 17 '15 at 17:44
  • Sorry i didn't reply for 2 days but i'm busy i haven't got the time yet to test it. i hope I wil tomorrow. – Willem Govaerts Nov 19 '15 at 20:26
  • Okay, Check it and let me know – Ashraful Islam Nov 19 '15 at 20:29
  • i've got some serious trouble when i try to change the targetsdkversion etc. to 21. When i change targetsdkversion, compileSdkVersion to 21 and buildToolsVersion to 21.1.2 i get the error: "Error retrieving parent for item: No recource found that matches the given name 'android:TextAppearance.Material.Widget.Button.invers' and "Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Button.Colored'. I've really tried everything but it seems i can't get it to work? – Willem Govaerts Nov 28 '15 at 17:29