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