Iam building an application that should recieve textMessage from specified number.
Is there anyway to recieve textmessage in an Edittext. here is the class for recieving sms:
class SMSReceiver extends BroadcastReceiver {
public static final String ACTION ="android.provider.Telephony.SMS_RECEIVED";
private static final String SMS_SENDER="123456789";
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null && intent.getAction() != null &&
ACTION.compareToIgnoreCase(intent.getAction()) == 0) {
Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
SmsMessage[] messages = new SmsMessage[pduArray.length];
for (int i = 0; i < pduArray.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]);
}
// SMS Sender, example: 123456789
String sms_from = messages[0].getDisplayOriginatingAddress();
//Lets check if SMS sender is 123456789
if (sms_from.equalsIgnoreCase(SMS_SENDER)) {
StringBuilder bodyText = new StringBuilder();
// If SMS has several parts, lets combine it :)
for (int i = 0; i < messages.length; i++) {
bodyText.append(messages[i].getMessageBody());
}
//SMS Body
String body = bodyText.toString();
// Lets get SMS Code
String code = body.replaceAll("[^0-9]", "");
}
}
}
}
You need to understand BroadcastReceiver by Android Developer Document.
In your case, you need to get the result from BroadcastReceiver to Activity, and then you can set the value to EditText.
You can refer to here.
Related
Hi all I'am trying to read sms body and source from a paricular port. I can read the source number but the sms body is always null. Can someone help me to resolve this?
#Override
public void onReceive(Context ctx, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
// Get received SMS Array
Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME);
for (int i = 0; i < smsExtra.length; i++) {
SmsMessage sms;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String format = intent.getExtras().getString("format");
sms = SmsMessage.createFromPdu((byte[]) smsExtra[i], format);
} else
sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
System.out.println(sms.getOriginatingAddress());
System.out.println(sms.getMessageBody());
}
}
}
}
I'm developing an android app with Android studio that should send user location via sms but I don't know how to start the app only when the app receives a specific text.
Instructions can be found here with one extra step that i outlined below in a TODO.
public class IncomingSms extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
//here you will get currentMsg body phoneNmber and senderNumber
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
//TODO launch app here!
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
}
I'm trying to read SMS on Android. Everything's fine except when I'm trying to access some value in SmsMessage object I'm getting an error:
12-29 17:41:58.762: E/AndroidRuntime(5545): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.android.internal.telephony.SmsMessageBase.getDisplayOriginatingAddress()' on a null object reference
12-29 17:41:58.762: E/AndroidRuntime(5545): at android.telephony.SmsMessage.getDisplayOriginatingAddress(SmsMessage.java:517)
I registered Receiver in AndroidManifest.xml, and by extending BroadcastReceiver created class SmsReceiver.
Method where I'm creating SmsMessage objects.
private final SmsMessage[] getMessagesFromIntent(Intent intent) {
Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
byte[][] pduObjs = new byte[messages.length][];
for (int i = 0; i < messages.length; i++) {
pduObjs[i] = (byte[]) messages[i];
}
byte[][] pdus = new byte[pduObjs.length][];
int pduCount = pdus.length;
SmsMessage[] msgs = new SmsMessage[pduCount];
for (int i = 0; i < pduCount; i++) {
pdus[i] = pduObjs[i];
msgs[i] = SmsMessage.createFromPdu(pdus[i]);
}
return msgs;
}
Method onReceive
#Override
public void onReceive(Context context, Intent intent) {
SmsMessage[] messagesArray = getMessagesFromIntent(intent);
for (SmsMessage message : messagesArray) {
String sender = message.getDisplayOriginatingAddress(); //the application fails here
String order = message.getMessageBody().toString();
//...
}
}
I've tried method Intents.getMessagesFromIntent(intent) but with no effect
I registered Receiver in AndroidManifest.xml, and by extending BroadcastReceiver created class SmsReceiver.
Since you haven't posted your AndroidManifest.xml i am assuming you are registering your receiver like this
<receiver android:name=". SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
if not please do it like this
as the Android Documentation for getMessagesFromIntent(Intent) says that it can only be used from API 19 onwards
hence ill tell you two methods to extract the sender's phone number and message from a received SMS
1. using getMessagesFromIntent(Intent) in your onRecieve method
for(SmsMessage message : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
if (message == null) {
Log.e(tag, "message is null");
break;
}
smsOriginatingAddress = message.getDisplayOriginatingAddress();
smsDisplayMessage = message.getDisplayMessageBody();
}
2. using PDU (protocol data unit) – the standard industry format for an SMS message
Object[] data = (Object[]) bundle.get("pdus");
for (Object pdu : data) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);
if (message == null) {
Log.e(tag, "message is null");
break;
}
smsOriginatingAddress = message.getDisplayOriginatingAddress();
smsDisplayMessage = message.getDisplayMessageBody();
}
this should work fine
I have some problem with passing throught my variable from Activity to the BroadcastReceiver...
Here is my code:
here is my Broadcast receiver code... I try to catch SMS from one phone number which I have got from my Activity...
public class SMSMonitor extends BroadcastReceiver
{
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
public static String phone_number = "";
public static String msg_body = "";
public static final String SMS_EXTRA_NAME = "pdus";
#Override
public void onReceive(Context context, Intent intent)
{
String phone = intent.getExtras().getString("trusted_num");
if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0)
{
Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
SmsMessage[] messages = new SmsMessage[pduArray.length];
for (int i = 0; i < pduArray.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]);
}
phone_number = messages[0].getDisplayOriginatingAddress();
msg_body = messages[0].getMessageBody();
System.out.println("Phone number: "+phone_number);
System.out.println("Phone entered: "+phone);
}
}
}
Here is my Activity code:
public class Settings extends Activity implements OnClickListener{
private Button btn_save;
private EditText txt_phone;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
//set Save button
btn_save = (Button)findViewById(R.id.btn_save);
txt_phone = (EditText)findViewById(R.id.et_phone);
btn_save.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_settings, menu);
return true;
}
#Override
public void onClick(View v)
{
if (v == btn_save)
{
try
{
String phone_num = txt_phone.getText().toString();
Intent i = new Intent(Settings.this, SMSMonitor.class);
i.putExtra("trusted_num", phone_num);
sendBroadcast(i);
}
catch(Exception e)
{
System.out.println("Error: "+e.getLocalizedMessage());
}
}
}
}
In this code I have text field for entering the phone number, which I need to pass to the BroadcastReceiver with intent.putExtra() method, but in LogCat I see, that variable didnot pass:
07-25 18:43:57.382: I/System.out(14245): Phone number: +37129690449
07-25 18:43:57.382: I/System.out(14245): Phone entered: null
So what I am doing wrong here?
UPD
Maybe code is not correct, but it works for me...
public void onReceive(Context context, Intent intent)
{
phone = intent.getExtras().getString("trusted_num");//get trusted phone number from Settings screen
//receive SMS
if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0)
{
Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
SmsMessage[] messages = new SmsMessage[pduArray.length];
for (int i = 0; i < pduArray.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]);
}
phone_number = messages[0].getDisplayOriginatingAddress();
msg_body = messages[0].getMessageBody();
System.out.println("Phone number: "+phone_number);
}
//check if number is not null
if (phone != null && phone != "")
{
System.out.println("Phone entered: "+phone);
}
}
}
You can't pass an intent to a broadcast receiver. "There is no way for a BroadcastReceiver to see or capture Intents used with startActivity()"
https://developer.android.com/reference/android/content/BroadcastReceiver.html
I had a similar issue a while back and solved it by using a combination of IntentServices and Activities. You have to restructure your program to fit these guidlines
Well, there are some things not that don't match:
You're sending an intent with no action in the first place, but you're specifying Broadcastreceiver's class; don't do like that:
Intent i = new Intent(Settings.this, SMSMonitor.class);
i.putExtra("trusted_num", phone_num);
sendBroadcast(i);
But try instead:
Intent i = new Intent("my_package_name.Some_general_constant");
i.putExtra("trusted_num", phone_num);
sendBroadcast(i);
Then, your BroadcastReceiver is supposed to know that it can also handle "Some_general_constant" action. For this reason, register an extra action in your Manifest file for your SMSMonitor:
<receiver android:name=".package_to_bla_bla.SMSMonitor">
<intent-filter>
<action android:name="my_package_name.Some_general_constant"/>
</intent-filter>
</receiver>
Then in your SMSMonitor you need to add an else if statement to handle this broadcast:
else if("my_package_name.Some_general_constant".equals(intent.getAction())
{
// get the data from intent and use it
}
I have a broadcast receiver SMSApp which onReceive reads bundleExtras from intent. Here, How can I read phone number from bundleExtras?
if(intent.getAction().equals(SMS_RECEIVED))
{
Bundle bundleExtras = intent.getExtras();
if (bundleExtras != null)
{
}
}
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
Now, messages[0].getOriginatingAddress() is the address and messages[0].getMessageBody() is the message body.
Tutorial at http://ukitech.blogspot.com/2014/11/android-sms-app.html
OUTPUT:
SmsReceiver.processReceivedSms﹕ SMS from +1650815xxxx
SmsReceiver.processReceivedSms﹕ SMS body Test 4
/**
* Created by uki on 11/22/14.
*/
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
final String tag = TAG + ".onReceive";
Bundle bundle = intent.getExtras();
if (bundle == null) {
Log.w(tag, "BroadcastReceiver failed, no intent data to process.");
return;
}
if (intent.getAction().equals(SMS_RECEIVED)) {
Log.d(tag, "SMS_RECEIVED");
String smsOriginatingAddress, smsDisplayMessage;
// You have to CHOOSE which code snippet to use NEW (KitKat+), or legacy
// Please comment out the for{} you don't want to use.
// API level 19 (KitKat 4.4) getMessagesFromIntent
for (SmsMessage message : Telephony.Sms.Intents.
getMessagesFromIntent(intent)) {
Log.d(tag, "KitKat or newer");
if (message == null) {
Log.e(tag, "SMS message is null -- ABORT");
break;
}
smsOriginatingAddress = message.getDisplayOriginatingAddress();
//see getMessageBody();
smsDisplayMessage = message.getDisplayMessageBody();
processReceivedSms(smsOriginatingAddress, smsDisplayMessage);
}
// Processing SMS messages the OLD way, before KitKat,
// this WILL work on KitKat or newer Android
// PDU is a “protocol data unit”, which is the industry
// format for an SMS message
Object[] data = (Object[]) bundle.get("pdus");
for (Object pdu : data) {
Log.d(tag, "legacy SMS implementation (before KitKat)");
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);
if (message == null) {
Log.e(tag, "SMS message is null -- ABORT");
break;
}
smsOriginatingAddress = message.getDisplayOriginatingAddress();
// see getMessageBody();
smsDisplayMessage = message.getDisplayMessageBody();
processReceivedSms(smsOriginatingAddress, smsDisplayMessage);
}
} // onReceive method
This is a link to a detailed "how to send and receive sms messages", you should check it, it is complete and full with examples:
http://mobiforge.com/developing/story/sms-messaging-android
also using this link, you can do:
if (bundle != null) {
SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
for (int i = 0; i < messages.length; i++) {
SmsMessage message = messages[i];
buf.append("Received SMS from ");
buf.append(message.getDisplayOriginatingAddress());
buf.append(" - ");
buf.append(message.getDisplayMessageBody());
}
}