I'm using a port for sending Data message in my app.
I have a problem with sending data message (sendDataMessage in android). my code works properly with MCI sim cards, I mean when the sender and receiver have MCI sim cards.
If sender has a MCI and receiver has an Irancell sim card message delivers properly.
On the contrary, if sender has an Irancell sim and receiver has a MCI sim it is not delivered at all :( (although in this case text message send/receive works properly, data message won't work).
Moreover when sender and receiver have Irancell sims it works properly.
please help me with this problem.
My manifest code:
<receiver android:name=".SmsListener">
<intent-filter android:priority="999">
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
<data android:scheme="sms" />
<data android:port="7442" />
</intent-filter>
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<meta-data android:name="port" android:value="7442" />
My smsListener:
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Yuhuuu", Toast.LENGTH_LONG).show();
MainActivity.smsReceived(true);
Bundle bundle = intent.getExtras();
String recMsgString = "";
String fromAddress = "";
SmsMessage recMsg = null;
byte[] data = null;
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdus.length; i++) {
recMsg = SmsMessage.createFromPdu((byte[]) pdus[i]);
try {
data = recMsg.getUserData();
} catch (Exception e) {
}
if (data != null) {
for (int index = 0; index < data.length; ++index) {
recMsgString += Character.toString((char) data[index]);
}
}
fromAddress = recMsg.getOriginatingAddress();
}
}
System.out.println(recMsgString + " ,this Has been Recieved from: " + fromAddress);
}
and my DataMessage sender code:
String messageText = "some text";
short SMS_PORT = 7442;
SmsManager smsManager = SmsManager.getDefault();
String phoneNumber = "+98910*******";
smsManager.sendDataMessage(phoneNumber, null, shortValue, messageText, null, null);
}
Related
I have an android application that receives and reads incoming SMS and I would like to start another activity when the message contains a specific word. I tried this but it does not work.
That is my Broadcasreceiver:
final SmsManager sms = SmsManager.getDefault();
String mobile, body;
String keyWord_code = "ovh";
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");
assert pdusObj != null;
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
mobile = phoneNumber.replaceAll("\\s", "");
body = message.replaceAll("\\s", "+");
Log.i("SmsReceiver", "senderNum: " + phoneNumber + "; message: " + body);
}
// Show Alert
if (body.contains(keyWord_code)){
context.startActivity(new Intent(context, Test.class));
//Toast toast = Toast.makeText(context, "senderNum: " + mobile + ", message: " + body, Toast.LENGTH_LONG);
//toast.show();
}else {
Toast toast = Toast.makeText(context, "senderNum: " + mobile, Toast.LENGTH_LONG);
toast.show();
}
} // end for loop
// bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
and here is the activity that I want to open:
public class Test extends AppCompatActivity {
private static final String TAG = "Test";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
The code above works at half. that is, when I receive a message, the application parses the message and displays the contents and number of the sender in a Toast but does not open Test.class!
that is my manifest:
<uses-permission android:name="android.permission.BROADCAST_SMS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:replace="android:icon">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".SMSListener"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity android:name=".Test"/>
</application>
I tried on Android KITKAT and OREO but it still does not work. I tried to open an AlertDialog end to allow the user to click himself to open Test.class but the AlertDialog is not displayed. I need help please!
I've found a few tutorials on how to send/receive text SMS messages, but none on how to send/receive data SMS messages. I have a very small amount of data I would like the users of my app to be able to share.
I am able to send, but my BroadcastReceiver doesn't ever get called. It seems this is a known issue (http://code.google.com/p/android/issues/detail?id=1576) but has anyone figured out how to do this yet?
I tried sending/receiving a text SMS and that works fine, the thing is, I need to specify a port so only my app can listen for the SMS.
It seems this question has been asked here before and was never answered: how to receive text sms to specific port..
I know this is 1 year old at time of my response, but I thought it could still help someone.
Receiving:
Bundle bundle = intent.getExtras();
String recMsgString = "";
String fromAddress = "";
SmsMessage recMsg = null;
byte[] data = null;
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i=0; i<pdus.length; i++){
recMsg = SmsMessage.createFromPdu((byte[])pdus[i]);
try {
data = recMsg.getUserData();
} catch (Exception e){
}
if (data!=null){
for(int index=0; index<data.length; ++index)
{
recMsgString += Character.toString((char)data[index]);
}
}
fromAddress = recMsg.getOriginatingAddress();
}
Setting up Receiver in Manifest:
<receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms" />
<data android:port="8901" />
</intent-filter>
</receiver>
Sending:
String messageText = "message!";
short SMS_PORT = 8901; //you can use a different port if you'd like. I believe it just has to be an int value.
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendDataMessage("8675309", null, SMS_PORT, messageText.getBytes(), null, null);
i am trying to build an app that response to the SMS of a specific number, for more demonstration i want a code like this:
if(number == +232344322)
{
do something
} else
{
do nothing
}
So what is the simplest way to do that in android ???
You need to create a BroadcastReceiver which will be invoked when an SMS is received.In the OnReceive of the BroadCastReceiver write the code retrive the number of the sms sender and compare the number and perform task based on that.
public class ReceiveSMS extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
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 message = currentMessage.getDisplayMessageBody();
Log.v("ranjapp", phoneNumber + " " + message);
if (phoneNumber.equals("+91xxxxxx"))) {
//do something
}else{
//do nothing
}
}
}
}
}
In the AndroidManifest.xml you need to add:
**The receiver with intent-filter within <application> tag
<receiver
android:name=".ReceiveSMS"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="android.provider.Telephony.SMS_DELIVER" />
</intent-filter>
</receiver>
Permission:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
I am trying to receive the sms from a particular number in my android application and trying to show the message content in my application. But i am not able receive the sms.
the class which i am using to receive is given below.
public class SmsReceiver extends BroadcastReceiver {
StringBuilder sb;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction()
.equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle extras = intent.getExtras();
if (extras != null) {
Object[] pdus = (Object[]) extras.get("pdus");
if (pdus.length < 1)
return; // Invalid SMS. Not sure that it's possible.
sb = new StringBuilder();
String sender = null;
for (int i = 0; i < pdus.length; i++) {
SmsMessage message = SmsMessage
.createFromPdu((byte[]) pdus[i]);
if (sender == null)
sender = message.getOriginatingAddress();
String text = message.getMessageBody();
if (text != null)
sb.append(text);
}
if (sender != null && sender.equals("********")) {
// Process our sms...
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS");
broadcastIntent.putExtra("data", sb.toString());
context.sendOrderedBroadcast(broadcastIntent, null);
System.out.println("MESSAGE FROM SERVER -->"
+ sb.toString());
this.abortBroadcast();
}
return;
}
}
}
}
After receiving the sms, i will check the sender and if it is the sender i am looking for then i will send another broadcast with the sms. There i will show the content.
see my manifest
<receiver android:name=".SmsReceiver" >
<intent-filter android:priority="2147483647" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="SMS" />
</intent-filter>
</receiver>
I am not able to find a solution please help.
Things to check (from my experience):
android:priority="2147483647" is not valid, the largest value is 1000. Numbers larger than this are ignored
Insert your this.abortBroadcast(); in the for loop (to be called pdus.length times)
If the sent SMS matches your criteria, for testing, save it in your app preference and in your main activity read the value from your preference, if it worked, then the problem is with your broadcastIntent.
I've found a few tutorials on how to send/receive text SMS messages, but none on how to send/receive data SMS messages. I have a very small amount of data I would like the users of my app to be able to share.
I am able to send, but my BroadcastReceiver doesn't ever get called. It seems this is a known issue (http://code.google.com/p/android/issues/detail?id=1576) but has anyone figured out how to do this yet?
I tried sending/receiving a text SMS and that works fine, the thing is, I need to specify a port so only my app can listen for the SMS.
It seems this question has been asked here before and was never answered: how to receive text sms to specific port..
I know this is 1 year old at time of my response, but I thought it could still help someone.
Receiving:
Bundle bundle = intent.getExtras();
String recMsgString = "";
String fromAddress = "";
SmsMessage recMsg = null;
byte[] data = null;
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i=0; i<pdus.length; i++){
recMsg = SmsMessage.createFromPdu((byte[])pdus[i]);
try {
data = recMsg.getUserData();
} catch (Exception e){
}
if (data!=null){
for(int index=0; index<data.length; ++index)
{
recMsgString += Character.toString((char)data[index]);
}
}
fromAddress = recMsg.getOriginatingAddress();
}
Setting up Receiver in Manifest:
<receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms" />
<data android:port="8901" />
</intent-filter>
</receiver>
Sending:
String messageText = "message!";
short SMS_PORT = 8901; //you can use a different port if you'd like. I believe it just has to be an int value.
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendDataMessage("8675309", null, SMS_PORT, messageText.getBytes(), null, null);