0

I followed a tutorial about listening for SMS in android, but I can't figure out why my code doesn't work. It compiles without errors, app runs, but I can't get toast to appear when message is recieved. Help will be appriciated :-D

MainActivity.java:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent listener = new Intent(this, ListenToSMS.class);
    listener.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startService(listener);

    Toast toasttest = Toast.makeText(this, "test" , Toast.LENGTH_LONG);
    toasttest.show();

    }
}

ListenToSMS.java:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.widget.Toast;


public class ListenToSMS extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(intent.getAction())) {
        for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
            String messageBody = smsMessage.getMessageBody();

            Toast toast = Toast.makeText(context, messageBody, Toast.LENGTH_LONG);
            toast.show();
            }
        }
    }
}

AndroidManifest.xml:

    <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">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".ListenToSMS">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

</application>

<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
ikurek
  • 604
  • 11
  • 27
  • Everything you've posted looks correct, so, provided you've run the app at least once after installation, you're probably coming up against Marshmallow's new permissions model. Have a look at [this post](http://stackoverflow.com/questions/32635704/cant-get-the-permission). – Mike M. Apr 12 '16 at 00:48
  • I can't find any errors in code too, will changing my API to lower (fe. Lollipop or KitKat) help in this case? – ikurek Apr 12 '16 at 00:54
  • 1
    Only if you're running under Marshmallow or above would the new permissions model be the problem. If that's the case, then setting your `targetSdkVersion` to 22 or below will cause your app to be granted permissions at installation, and you won't have to request then at runtime. So, yes, setting that to something less than 23 will probably fix your issue, but you'll eventually want to update your app to be in line with the new model. – Mike M. Apr 12 '16 at 00:59
  • Thank you, I will try to figure it out using requestPermission() – ikurek Apr 12 '16 at 01:09
  • Sure thing. It wouldn't hurt anything, btw, to go ahead and temporarily set the `targetSdkVersion` to <=22 to make sure your current setup works. You can always change it back after you get the runtime permissions stuff down. Cheers! – Mike M. Apr 12 '16 at 01:11

0 Answers0