Davide
Davide

Reputation: 2124

BroadcastReceiver for incoming SMS on Samsung-Android based smartphone

I checked every stackoverflow post before posting this problem because they didn't help. I'm using this documentation https://developer.android.com/reference/android/provider/Telephony?hl=it to understand how to build an application that listen for incoming SMS, but it seems it doesn't work in my case, so as all stackoverflow posts.

Following is what I just developed:

AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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=".IncomingSMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>


MainActivity.java

package abc;

import android.Manifest;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SEND_SMS},1);
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_SMS},2);
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.RECEIVE_SMS},3);
    }
}


IncomingSMS.java

package abc;

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

public class IncomingSMS extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("It works!");
    }
}

Unfortunately, when I receive a new SMS, it doesn't write "It works!"

In that website there is this section:

Note: These APIs are not available on all Android-powered devices.

I'm resigning and I'm really thinking that the Samsung Galaxy A5 2017 at my disposal does not allow me to do what I want.
Is there something in the code that I forgot or really I can't develop this application on this phone?

Upvotes: 0

Views: 805

Answers (1)

Davide
Davide

Reputation: 2124

Finally after two days of work I was able to understand the problem. If you deactivate and re-enable SMS permissions, the problem is resolved. This clearly a bug on Android Oreo.

Even better: I modified the code as following:

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS, Manifest.permission.RECEIVE_SMS, Manifest.permission.READ_SMS},1);

Upvotes: 1

Related Questions