the following program is my sample program for Listening incoming sms.It is created .apk file
with out error but it does not display the message please help me.the toast does not display
any message if the emulator receive the message.
My scenario is receive the sms ansd display the alert dialog box to user.that sms contanins
email address depending on that address my app search the phone contacts and send the contact
number of the emailId's person as reply message
public void onReceive(Context context,Intent intent)
{
Bundle extras=intent.getExtras();
String messages="";
if(extras!=null)
{
Object[] smsExtra=(Object[]) extras.get("pdus");
for(int i=0;i<smsExtra.length;i++)
{
SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
String body = sms.getMessageBody().toString();
String address = sms.getOriginatingAddress();
messages += "SMS from " + address + " :\n";
messages += body + "\n";
}
Toast.makeText(context, messages, Toast.LENGTH_SHORT).show(); // not display
}
}//onReceive
my manifastfile
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="e.x.x"
android:versionCode="1"
android:versionName="0.1" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<receiver android:name=".ex2" android:exported="true" >
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</manifest>
I'm not sure about manifest. Try like in this example Android – Listen For Incoming SMS Messages
I think android:priority="999" and android:exported="true" is a root of problem
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'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();
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've followed google instruction on initiating a gcm server and client.
my problem is though i get a successful gcm from server :
MulticastResult(multicast_id=8287827393174436535,total=1,success=1,failure=0,canonical_ids=0,results:
[[ messageId=0:1366468335181772%8e1522ac00000031 ]]
i'm not able to see any movement of receiving a message on the client - onMessage() isn't called.
client id as well as server id are correct as far as i can tell.
i'd appreciate any help... :)
the server side code :
try {
Sender sender = new Sender(API_KEY);
Message message = new Message.Builder().collapseKey("1")
.timeToLive(3)
.delayWhileIdle(true)
.addData("message",
"this text will be seen in notification bar!!").build();
ArrayList<String> devices = new ArrayList<String>();
devices.add(DEVICE_ID1);
//devices.add(DEVICE_ID2);
MulticastResult result = sender.send(message, devices, 2);
//Result result = sender.send(message, DEVICE_ID1, 2);
System.out.println(result.toString());
if (result.getResults() != null) {
int canonicalRegId = result.getCanonicalIds();
if (canonicalRegId != 0) {
}
} else {
int error = result.getFailure();
System.out.println(error);
}
} catch (Exception e) {
// TODO: handle exception
}
the client code :
registering :
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, SENDER_ID); // Note: get the sender id from configuration.
String regIdString = GCMRegistrar.getRegistrationId(this);
Log.v(TAG, "RegisterId, regId: " + regIdString);
} else {
Log.v(TAG, "Already registered, regId: " + regId);
}
and gcmIntentServiceCreated (partly) :
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService(){
super(SENDER_ID);
}
#Override
protected void onMessage(Context context, Intent intent) {
Log.i("Registration", "Got a message!");
Log.i("Registration", context.toString() + " " + intent.toString());
}
...
}
client's manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission android:protectionLevel="signature" android:name="com.example.myapp.permission.C2D_MESSAGE"></permission>
<uses-permission android:name="com.example.myapp.permission.C2D_MESSAGE"/>
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myapp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.myapp" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
</manifest>
To maximise your chances of receiving the message, I would not set the time_to_live at a mere 3 seconds. I'd omit this parameter altogether so that it takes the default value of 4 weeks. I would also set delay_while to false, so that the phone gets the message even whilst idle.
Well your message is surely getting sent to your device since gcm sends a success status of 1. Try to check the GCMReceiver code of your app.
Change the following code in your onMessage method:
Log.i("Registration", "Got a message!");
Log.i("Registration", context.toString() + " " + intent.toString());
to this:
String message=intent.getStringExtra("message");
Log.w("The message received is", message);
hope this help
I wrote send and received sms in android... I checked received phone number with special number(a phone number that received sms just it) in onReceive method, but this program opened for every phone number that sent sms!!!! but I dont want it!!!!!! my question is that broadcast receiver class for every received sms from every phone number that opened and automatically running?
public class SmsReceiver extends BroadcastReceiver {
public String str = "";
#Override
public void onReceive(Context context, Intent intent) {
// ---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
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]);
//for get sms from special number===============================
String msg_from = msgs[i].getOriginatingAddress();
Log.v("msg_from >>",msg_from);
if(msg_from.equals("08522215"))
{
//===============================
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
}
// ---display the new SMS message---
// Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Intent act = new Intent(context, MainActivity.class);
act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
act.putExtra("message", str);
context.startActivity(act);
}
abortBroadcast();
}
}
manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name">
<activity android:name=".SMS"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:label="#string/app_name"/>
<receiver
android:name="com.example.sms.SmsReceiver"
class="com.example.sms.SmsReceiver" >
<intent-filter android:priority="100" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
I think it's because you use this code:
if(msg_from.equals("08522215"))
You should set your number completely (0098913...) or perhaps you can use this code
if(msg_from.contain("08522215")