Re: XPA 3.2b Android SMS Listener


Todd Baremore
 

Brenda,
WOW!  What a great way to start a Monday.  Give me a few days to try it and I'll let you know if I run into any problems.
Thank you very much  -  Muchas Gracias

If I ever find myself in Buenos Aires, pick a nice restaurant, dinner is on me.  Bring Nicolas.

Todd


On 6/11/2017 8:59 PM, Brenda Bullorini wrote:

Hi Todd, just tested in Android Studio and also in Magic.

package com.magicsoftware.magicdev;
 
import com.magicsoftware.core.CoreApplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.widget.Toast;
 
public class MySmsListener extends BroadcastReceiver {
    
    final SmsManager sms = SmsManager.getDefault();
 
    public void onReceive(Context context, Intent intent) {
 
//Toast.makeText(context, "I got to onReceive", Toast.LENGTH_SHORT).show();
 
        final Bundle bundle = intent.getExtras();
 
        try {
 
            if (bundle != null) {
 
                final Object[] pdusObj = (Object[]) bundle.get("pdus");
 
                for (int i = 0; i < pdusObj.length; i++) {
 
                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();                    
CoreApplication.getInstance().invokeExternalEvent("SMSRECEIVED:" + message);
 
                } 
            } 
 
        } catch (Exception e) {
            // Error
//Toast.makeText(context, "Exception" + e.getMessage(), Toast.LENGTH_SHORT).show();
CoreApplication.getInstance().invokeExternalEvent("SMSERROR:" + e.getMessage());
        }
    }
}

On the AndroidManifest.xml add the following lines:
<receiver android:name=".MySmsListener">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
 </receiver>

Build apk.


Then in Magic:



Thinks to keep in mind:

- I added a Toast in the java class, uncomment if you need to see if the sms gets to the android java class.
- Since android >= API 23 needs a different type way of requestion permissions, I use targetSdkVersion 22 on the app build.gradle. I tested this on my cellphone that has android 6.0 and it's working.
- I think that's all!

Let me know if you need any help!!

Brenda


 

Join main@magicu-l.groups.io to automatically receive all group messages.