I'm creating app using flash cs 6. I need to send sms from my app. I created the native extension which will send the sms. It uses default sms manager.It is working fine. But when i use it to send sms it prompt me a message that it may cause charges on your mobile account. Is there a way to send sms without that message ? I heard some app ask if the app can send sms or not at the beginning of the installation. And when user agree to send sms from app, app never promt that message. Is there a way to do that ? And I also want the app to read the sms that send from me.
I used following code for sending sms:
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(recipient, null, text, null, null);
Now I also can read sms using following code:
Uri uri = Uri.parse("content://sms/inbox");
Cursor c= context.getActivity().getContentResolver().query(uri, null, null ,null,null);
c.moveToFirst();
String body = c.getString(c.getColumnIndexOrThrow("body")).toString();
c.close();
Is there a way to receive the sms in app when the device first receive the sms? I want to read the sms send from me/server only.
You probably want to look into the following permissions:
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
To receive an SMS when it arrives at the device you need to setup a receiver and add the SMS_RECEIVED intent filter:
<receiver android:name="YourSMSReceiverClass" android:exported="true" >
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
In your receiver you can then process the SMS for your needs:
public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
public static final String SMS_EXTRA_NAME = "pdus";
#Override
public void onReceive( Context context, Intent intent )
{
if (SMS_RECEIVED_ACTION.equals(intent.getAction()))
{
Bundle extras = intent.getExtras();
if ( extras != null )
{
Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME );
for ( int i = 0; i < smsExtra.length; ++i )
{
SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
String message = sms.getMessageBody().toString();
String address = sms.getOriginatingAddress();
// Here you can add any your code to work with incoming SMS
}
}
}
}
Also in your receiver you can stop the SMS from being put to incoming by calling:
this.abortBroadcast();
Related
Is it possible in android to filter the SMS from a specific number in android.And save it in a specific database.and the SMS should not be visible in the inbox...
You have yo do this in 3 step:
read SMS from Specific number you have to look in to this link then after
you have to create Database for store all info about SMS(like id, body, number, time) for that check this link
you have to Delete SMS from INBOX check this link for delete SMS.
add below permission for do all step:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
ya is it possible.
in mainfest file add following code:
<receiver android:name="com.example.Sms_BReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
after that create a class use following code
public class Sms_BReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] messageString = null;
Object[] pdus = (Object[]) bundle.get("pdus");
messageString = new SmsMessage[pdus.length];
for (i = 0; i < messageString.length; i++) {
messageString[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
Phone_no = messageString[0].getOriginatingAddress();
Message_body = messageString[0].getMessageBody();
Time = messageString[0].getTimestampMillis();
CharSequence text = str;
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}
if (Phone_no.contains(here check no to filter) {
here delete message form inbox
and save in our own db
} else {
}
}
i hope this code will use full for u
I am trying to use a broadcast receiver to read incoming SMS in my app and also able to do so. However, if someone receives SMS on google hangout instead of default SMS app, the broadcast receiver doesn't work.
The following is the code I am using:
public class SmsListener 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++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,
"senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
The manifest has:
<receiver android:name=".SmsListener">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Permissions:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
Did you add this permission?
<uses-permission android:name="android.permission.RECEIVE_SMS" />
You may want to set the android priority value to a suitable value.
Ordered broadcasts (sent with Context.sendOrderedBroadcast) are delivered to one receiver at a time. As each receiver executes in turn, it can propagate a result to the next receiver, or it can completely abort the broadcast so that it won't be passed to other receivers. The order receivers run in can be controlled with the android:priority attribute of the matching intent-filter; receivers with the same priority will be run in an arbitrary order.
Is it possible to read incoming sms and reply back only to specific or desire number. i-e i want to make app that will run in back ground ... whenever i will send sms with my number it will automatically respond back to me with XYZ information
Yes, it's possible. You need to add a BroadcastReceiver in your app that will intercept incoming SMS messages and then send a message if it matches the number you're looking for.
Following source code from:
BroadcastReceiver + SMS_RECEIVED
http://shreymalhotra.me/blog/tutorial/receive-sms-using-android-broadcastreceiver-inside-an-activity/
In AndroidManifest.xml:
<receiver android:name=".SMSReceiver">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
In a new file called SMSReceiver.java:
public class SMSReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
// get SMS data, if bundle is null then there is no data so return
Bundle extras = intent.getExtras();
if (extras == null) return;
// extract SMS data from bundle
Object[] pdus = (Object[]) extras.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
String sender = SMessage.getOriginatingAddress();
String body = SMessage.getMessageBody().toString();
// if there's an SMS from this number then send
if(sender.equals("+1800555555") {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(sender, null, "sms received", null, null);
}
}
}
}
I am writing an app that has a feature of sending and receiving SMS messages. I would like SMS messages sent by my app be only received by another phone with my app installed and not reach phone's native SMS app. The only solution I found so far is to set high priority to android:priority in Manifest file:
<receiver android:name=".SMSreceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
and put this.abortBroadcast() statement in onReceive method to the class that extends BroadcastReceiver:
public class SMSreceiver extends BroadcastReceiver
{
private final String CODE = "myappcodeword";
#Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String from = "";
String str = "";
String sms = "";
if (bundle != null)
{
Object [] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i<msgs.length; i++)
{
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
if (i == 0)
{
from = msgs[i].getOriginatingAddress();
}
str += msgs[i].getMessageBody().toString();
}
if (str.startsWith(CODE)) // SMS starts with code word therefore was sent by my app
{
sms = str.substring(CODE.length()); // extract code word from actual sms message
Intent SMSIntent = new Intent(context, SMSActivity.class);
SMSIntent.putExtra("from", from);
SMSIntent.putExtra("sms", sms);
SMSIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(SMSIntent); // start new activity using sms info
this.abortBroadcast();
}
}
}
}
Since of course I don't want to block SMS not intended for my app, I thought of starting SMS messages sent by my app by some kind of key word. My question is, is there a better solution than this? (For example, user might theoretically actually send SMS to somebody containing key word on purpose no matter how random the key word is and my app would filter it out). Also above code will not filter out the SMS sent by my app if the phone the SMS is sent to does not have my app installed. Is there a way to find out first if the phone SMS is being send to has my app installed? If the app is not installed then I would like the SMS not even show up on that phone.
you might want to look at GCM, allows you to target your application running on targeted devices based on your need.
Google Cloud Messaging for Android
No, you cannot use SMS as a carrier for the scenario you describe. Remember that SMS is a store and forward service (in that respect similar to email) and the mobile operator might be storing the message for a long time (days is possible) if the recipient is not turned on or out of coverage (at some point it will be discarded, but that is up till the operator to decide).
I want implement an app which receives SMS on a specific port.
Manifest Code:
<receiver android:name=".BinarySMSReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED"/>
<data android:port="8091"/>
<data android:scheme="sms"/>
</intent-filter>
</receiver>
And receiver class code below.
public class BinarySMSReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if(null != bundle)
{
String info = "Binary SMS from ";
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
byte[] data = null;
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
info += msgs[i].getOriginatingAddress();
info += "\n*****BINARY MESSAGE*****\n";
data = msgs[i].getUserData();
for(int index=0; index<data.length; ++index)
{
info += Character.toString((char)data[index]);
}
}
Toast.makeText(context, info, Toast.LENGTH_SHORT).show();
}
}
}
I am getting all SMS which are on this (8091) port or not. I want to receive only those messages which are port specific.
For those who are still wondering why the app is receiving data SMS directed to other ports and not just to port 8091. The problem lies in the Manifest code. I've seen many solutions that were posted and most of them are actually wrong.
The Manifest should be :
<receiver
android:name = ".SmsReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data
android:scheme="sms"
android:host="*"
android:port="8095" />
</intent-filter>
</receiver>
The scheme, host and port attributes must be defined in only one "data" element of the intent-filter, not in separate "data" elements.
Also not that from this link, under "data test" section it states that
"The host and port together constitute the URI authority; if a host is not specified, the port is ignored."
So keep in mind that you need to specify the host if you want the app to receive the data SMS for only a specific port.
The "host" element is * (asterisk) is to specify that it accepts data SMS from all hosts / phone numbers
Hope this helps someone (:
I had similar problem, just add following check condition at begin of your 'onReceive' code:
String dataStr = intent.getDataString();
if (dataStr.indexOf(":8888" ) == -1) {
return;
}