0

I have searched for a few hours and found similar problems to mine and tried those solutions, but nothing has worked so far. I am using android studio with the emulator running android version 23. I suspect that my problem is somewhere with the permissions for SMS_RECEIVE as my other actions are triggering my receiver's behavior. this is my manifest:

<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=".SmsMonitor" android:exported="true" android:permission="android.permission.BROADCAST_SMS" >
        <intent-filter android:priority="999" >
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            <action android:name="myAction.updateKeyword" />
            <action android:name="myAction.closeApp" />
    </intent-filter>
    </receiver>

</application>

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

and this is the code for my receiver:

package com.example.the_f.smsalert;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.app.NotificationCompat;
import android.telephony.SmsMessage;

import static android.R.drawable.stat_notify_more;

/**
 * Created by the_f on 11/10/2016.
 */

public class SmsMonitor extends BroadcastReceiver {

public static final String SMS_BUNDLE = "pdus";
private final String UPDATE_KEYWORD = "myAction.updateKeyword";
private final String CLOSE_APP = "myAction.closeApp";
private final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private Context context;

@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;
    String action = intent.getAction();
    System.out.println("action is: " + action);

    //only want to inspect the message if the app is active
    if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
        System.out.println("the action was sms received");
        Bundle extras = intent.getExtras();
        if(extras != null){
            Object[] messages = (Object[]) extras.get(SMS_BUNDLE);
            String messageContent = "";
            for(int i = 0; i < messages.length; i++){
                SmsMessage msg = SmsMessage.createFromPdu((byte[]) messages[i]);
                messageContent = msg.getMessageBody().toString();
            }

            System.out.println("build sms message: " + messageContent);
            inspectForKeyword(messageContent.toLowerCase());
    }

    //update the keyword for the app. also sets the receiver to active to start monitoring
    }else if(action.equals(UPDATE_KEYWORD)){
        keyword = intent.getStringExtra("keyword").toLowerCase();
        active = true;
        System.out.println("changing keyword to: " + keyword);
        testAlarm();

        //the main activity was closed and so this receiver should stop monitoring
    }else if(action.equals(CLOSE_APP)){
        System.out.println("setting smsMonitor in inactive");
        active = false;

    }else{
        //not a valid intent for this receiver. shouldnt get here.
    }


}

private void inspectForKeyword(String messageContent) {
    if (messageContent.contains(keyword)) {
        //activate vibrate

        //activate ringer

        //activate led flash

        //send notification

    }
}

private void testAlarm(){
    //binds an activity to start when the notification is clicked
    Intent smsIntent = new Intent(Intent.ACTION_MAIN);
    smsIntent.setType("android.intent.category.APP_MESSAGING");
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 , smsIntent, 0);

    //builds the notification
    NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context);
    notifBuilder.setSmallIcon(stat_notify_more);
    notifBuilder.setContentTitle("You have an Emergency Alert!");
    notifBuilder.setContentText("Check your messages for an Emergency!");
    notifBuilder.setContentIntent(pendingIntent);
    //sends notification
    NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notifManager.notify(0, notifBuilder.build());

    Vibrator vibes = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    vibes.vibrate(3000);


}

}

most of the java code can be ignored. I have a bunch of code that i was trying to test with. However, at the top of onReceive() i have a print statement to print the name of the action. The print statement triggers when i use the other two actions, but not when i use the emulator and simulate it receiving a text. The emulator's messaging app is indicates that it receives a message when i simulate sending an sms, but my broadcastreceiver isnt being notified. I could really use some help with this. Im clueless as to what my problem is.

  • 1
    You don't need to specify `android:permission="android.permission.BROADCAST_SMS" ` in your ``. I don't think that will solve your problem, but you don't need it, so you can remove it and see if that helps. Only System apps are permitted to broadcast the `SMS_RECEIVED` `Intent`. You also don't need `exported="true"` in your `` declaration, because "exported" is automatically set to "true" if you have an ``. – David Wasser Nov 12 '16 at 21:06
  • 1
    Have a look here, too: http://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it. – Mike M. Nov 12 '16 at 21:10
  • Ill try that out Mike and report back. Thanks – Brandon Johnson Nov 12 '16 at 21:20
  • 1
    @MikeM. thank you so much, its working now. i never would have figured out that they made that permissions change in 6.0. can finally move forward with this. – Brandon Johnson Nov 12 '16 at 22:07
  • No problem. I went ahead and closed out your question, since you got it working. Cheers! – Mike M. Nov 12 '16 at 22:11

0 Answers0