2

Alright, I have tried every solution on Stack and nothing works.My current method registers the "SmsListener" receiver from the MainActivity. All I'm trying to do is initialize the onReceive method. There are no errors; It simply isn't picking up a broadcast. What am I doing wrong? Pasting the applicable code here. Anything else that may be needed please just ask.

Update: Here is a similar unresolved issue Listen Android incoming SMS when Google Hangout or other app receives it I am testing under Android 6.0.1. Target Sdk version is 22. Min Sdk is 19. It's worth noting that I just tested my original code on an LG Optimus GPro with Android 4.4.2 and it worked. It still isn't working on my Nexus with Android 6.0.1.

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.biapps.makin_biscuits">
<uses-sdk android:minSdkVersion="4" />

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_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">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action `android:name="android.service.notification.NotificationListenerService" />`
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

    <receiver
        android:name=".SmsListener"
        android:priority="999"
        android:enabled="true"
        android:exported="true">

    </receiver>

    <receiver
        android:name=".IncomingCallReceiver"
        android:enabled="true"
        android:exported="true">

    </receiver>


</application>

Main Activity

    package com.biapps.makin_biscuits;

    import android.service.notification.NotificationListenerService;
    import android.app.NotificationManager;

    import android.content.Context;

    import android.content.IntentFilter;
    import android.media.AudioManager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.content.Intent;

    import android.view.View;
    import android.widget.ImageButton;

    import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    //set object labels and states here
    private ImageButton icon;
    private AudioManager am;
    private ImageButton people;
    private ImageButton ring;
    private NotificationManager nm;
    private NotificationListenerService nls;
    IncomingCallReceiver broadCastReceiver = new IncomingCallReceiver();
    SmsListener smsReceiver = new SmsListener();
    @Override

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

        am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        icon = (ImageButton) findViewById(R.id.icon);
        icon.setOnClickListener(imgButtonHandler);
        people = (ImageButton) findViewById(R.id.people);
        //people.setOnClickListener(peopleButtonHandler);
        ring = (ImageButton) findViewById(R.id.ring);

    }

    int buttonstate = 0;
    public View.OnClickListener imgButtonHandler = new View.OnClickListener() {

        public void onClick(View v) {
            if (buttonstate == 0) {

                icon.setImageResource(R.drawable.buttonup);
                buttonstate = 1;
                am.setRingerMode(0);

                registerReceiver(broadCastReceiver, new IntentFilter(
                        "android.intent.action.PHONE_STATE"));
                registerReceiver(smsReceiver, new IntentFilter(
                        "android.intent.action.DATA_SMS_RECEIVED"));
                registerReceiver(smsReceiver, new IntentFilter(
                        "android.provider.Telephony.SMS_RECEIVED"));
                registerReceiver(smsReceiver, new IntentFilter(
                        "android.provider.Telephony.DATA_SMS_RECEIVED"));
                Toast.makeText(getApplicationContext(),"Diving!", `Toast.LENGTH_SHORT)`
                        .show();

            } else {

                icon.setImageResource(R.drawable.button);
                buttonstate = 0;
                am.setRingerMode(2);

                unregisterReceiver(broadCastReceiver);
                unregisterReceiver(smsReceiver);
                Toast.makeText(getApplicationContext(),"Surfacing!", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    };}

SmsListener

package com.biapps.makin_biscuits;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
import android.telephony.TelephonyManager;


public class SmsListener extends BroadcastReceiver {

private static final String TAG = "SmsListener";

public static final String SMS_BUNDLE = "pdus";
@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "ON SMS RECEIVE BROADCAST", Toast.LENGTH_LONG).show();
    Log.i(TAG, "SmsListener - onReceiveCalled");

}}
Community
  • 1
  • 1
metamonkey
  • 427
  • 7
  • 33
  • Is your `MainActivity` running in the foreground when the SMS is received? What's your `targetSdkVersion`, and which version of Android are you testing under? – Mike M. May 04 '16 at 06:26
  • I am testing under 6.0.1. Target Sdk version is 22. Min Sdk is 19. Yes MainActivity is in the foreground. It's worth noting that I just tested my original code on an LG Optimus GPro with Android 4.4.2 and it worked. It still isn't working on my Nexus with Android 6.0.1. – metamonkey May 04 '16 at 07:34
  • Similar unresolved issue: http://stackoverflow.com/questions/32022569/listen-android-incoming-sms-when-google-hangout-or-other-app-receives-it – metamonkey May 04 '16 at 21:45
  • Most likely not. If Hangouts is causing the problem there, it's because it's intercepting and aborting the broadcast before that user's Receiver gets it. Your Nexus is running Marshmallow. The `SMS_RECEIVED` broadcast cannot be aborted in that version. – Mike M. May 04 '16 at 22:12
  • Interesting. Do you think it could have something to do with using Google Fi phone service? – metamonkey May 04 '16 at 22:35
  • Dunno. It's possible, if Hangouts uses some other protocol for text messaging while on Google Fi, but I'm not at all familiar with Fi. – Mike M. May 04 '16 at 22:47

4 Answers4

7

Try following way with highest reading priority value,

<receiver android:name=".SmsListener"
             android:enabled="true"
            android:exported="true"
            android:permission="android.permission.READ_SMS">
    <intent-filter android:priority="2147483647">
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
</receiver>

This will surely solve out your problem.

Update from below comment,

Since you are checking on Android version 6.0.1 just follow these steps,

  1. Go to Settings,
  2. Go to Apps
  3. Select your Application
  4. Choose Permissions Option
  5. Enable SMS permission
Arjun
  • 736
  • 1
  • 8
  • 24
  • I tried this. It still doesn't work. It's worth noting that I just tested my original code on an LG Optimus GPro with Android 4.4.2 and it worked. It still isn't working on my Nexus with Android 6.0.1. – metamonkey May 04 '16 at 07:32
  • I have a feeling another app is taking priority and blocking broadcasts, but I've uninstalled both Google Hangouts and Facebook messenger and still can't get it to run. I also queried Receivers and only returned 05-04 01:20:13.141 31026-31026/com.biapps.Diive I/System.out: Receiver name:com.google.android.gms.icing.proxy.SmsMonitor; priority=0. – metamonkey May 04 '16 at 07:40
  • Try unlistall & re-install your application in Nexus. It must work. – Arjun May 04 '16 at 07:42
  • Do you use Google Hangouts on your nexus 5 that is working? It appears that in my Nexus all messages get filtered to Hangouts whether I choose it or not. Could this be causing the issue? @Arjun – metamonkey May 04 '16 at 19:18
  • @Jake, Yes I do use Hangout. No that can not be an issue. – Arjun May 05 '16 at 03:35
  • It can't? How do you know? – metamonkey May 05 '16 at 21:14
  • well because I too developed same SMS reader code and it is working find in my Nexus Phone. – Arjun May 06 '16 at 07:03
  • http://stackoverflow.com/questions/20021492/enabling-sms-support-in-hangouts-2-0-breaks-the-broadcastreceiver-of-sms-receive – metamonkey May 09 '16 at 18:36
  • Well I do have hungout in my Nexus 5 , but code for SMS reading is working fine. – Arjun May 10 '16 at 04:51
  • enabling the sms permission did it for me. I didn't know I had to go to apps to do it. – AbbasFaisal Jan 22 '17 at 14:48
  • applying this setting worked for me but if we already have granted the permission why the hell is this needed? – Behnam Esmaili Jul 20 '17 at 08:45
6

After spending more than an hour I found that RECEIVE_SMS permission is required.

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

Prioirty is not required to be set. This should work.

nadafafif
  • 561
  • 6
  • 10
0

You are registering broadcast your in your Activity, so it won't work if your app is in background. you can remove this from your Activity and you can register it in Manifest.

ex:

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

Along with this add permission to recieve sms.

Try this, and it will work

droidev
  • 7,352
  • 11
  • 62
  • 94
  • I tried this. It still doesn't work. It's worth noting that I just tested my original code on an LG Optimus GPro with Android 4.4.2 and it worked. It still isn't working on my Nexus with Android 6.0.1. – metamonkey May 04 '16 at 07:31
  • how you are testing this ? – droidev May 04 '16 at 09:45
  • I've been using Run 'app' in android studio with my Nexus connected. I also generated a signed APK and sent it to myself to assure that that wasn't the issue. – metamonkey May 04 '16 at 17:31
  • I mean how you are receiving sms ? Sending sms from other device or you are using adb command or you are using any fake sms tool ? – droidev May 04 '16 at 18:16
  • Also go through http://developer.android.com/training/permissions/requesting.html – droidev May 04 '16 at 18:19
  • I am sending messages from another device! – metamonkey May 04 '16 at 21:50
  • I went through that documentation and didn't come to a solution. – metamonkey May 04 '16 at 22:34
  • Similar unresolved issue: http://stackoverflow.com/questions/32022569/listen-android-incoming-sms-when-google-hangout-or-other-app-receives-it – metamonkey May 04 '16 at 22:34
0

Found a solution.

First make another app your default SMS app.

Then: Google Hangout --> Settings (Disable merged conversations) --> SMS (Disable SMS)

metamonkey
  • 427
  • 7
  • 33