Re: XPA 3.2b Android SMS Listener


Todd Baremore
 

Brenda,

Its not working.....yet.

I saved your java code to:
C:\mse\xpa_32\RIAModules\Android\Source\app\src\main\java\com\magicsoftware\magicdev\MySmsListener.java

I have two questions that should narrow down the problem:
1.  Where exactly should I update AndroidManifest.xml?  I placed your update at the bottom of the file.  Should it be within an Activity? I bolded your content below
      ...............      
       <activity android:name="SettingsActivity"/>
        <!-- [START firebase_service] -->
        <service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <receiver
            android:name=".MyFirebaseNotificationDismissReceiver"
            android:exported="false" >
        </receiver>

        <receiver
            android:name=".MyFirebaseNotificationClickReceiver"
            android:exported="false" >
        </receiver>
        <!-- [END firebase_service] -->
        <!-- [START firebase_iid_service] -->
        <service
            android:name=".MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>
        <!-- [END firebase_iid_service] -->

<receiver android:name=".MySmsListener">

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

    </application>
</manifest>

2.  I am able to successfully compile an APK using SDK 21, 22 and 23 but I receive the message below for all 3:
Note: C:\mse\xpa_32\PublishedApplications\EMP_Mobile\Android\source\TEMP_BUILD_FOLDER\app\src\main\java\com\todd\emp\MySmsListener.java uses or overrides a deprecated API.

Let me know if you need any more info.

Thanks

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.