My application uses some web services for performing actions in server and getting results from it. Now I want my app to work in offline mode. I have a sms panel that can handle sending and receiving messages to clients. So the thing I need is to listen to messages in client and respond to it if it has a special format. I know how to listen to incoming sms from code, here it is:
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// ---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
Log.d("checking", "sms received!");
String str = "";
if (bundle != null) {
// ---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
String sender = null;
String body = null;
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
sender = msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
body = msgs[i].getMessageBody().toString();
str += "\n";
}
// ---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
and it works properly. The thing here is I don't want my special messages appear in user inbox or even hide sms notification from him. Is this possible? Should I listen to all incoming messages?
update:
I assume that things are different in KitKat so any solution works in api 19 appreciated.
thanks in advanced.
Try abortBroadcast(); in your BroadcastReceiver
if (intent.getAction().equals(android.provider.Telephony.SMS_RECEIVED)) {
abortBroadcast();
}
i don't know if this works on KitKat or not.
Related
I want to open incoming sms from notification directly to my application's intent , Please help I tried following code to read sms from inbox
public class SmsReceiver extends BroadcastReceiver
{
public static String BODY = "Test";
public static String ADDRESS = "5556";
#Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
ADDRESS=msgs[i].getOriginatingAddress();
BODY=str;
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
When a new SMS is received by your device, the android creates a intent and broadcasts the SMS_RECEIVED_ACTION. What you are going to do is create an Intent Filter for the activity of your application that handles the SMS that the phone receives.
When the phone gets a new SMS message and the user clicks on the notification, she is presented with a dialog of apps that can handle this action*****. Your app should be there, and when she decides that she wants your app to handle the incoming message then you can use the getMessagesFromIntent method in order to extract the message (you use that method inside your app).
PS: You cannot set your app as the default message app, which you probably need to do. The user has to explicitly choose your app to open the message, and if she wants it to be the default messaging app (by ticking the set as default checkbox).
Note: It looks like you have to create a broadcast receiver that receives the SMS_RECEIVED_ACTION which you can use prior to API 19 as a constant at android.provider.Telephony.SMS_RECEIVED and launch your own notifications in which you direct the user directly to the application of yours. Unfortunately it seems that the default messaging app will still issue notifications.
Credit: #Mike M.
I search so much for sms filter for hangout enabled android phones where I only found that this is not possible.
But there is one app as sms filter by Tsvetan Nachev which blocks sms from hangout and link for this app is : https://play.google.com/store/apps/details?id=com.nachev.apps.smsfilter
I tested on 4.1.2
So anybody know how does that happening in that app?
here is my code
if (intent.getAction().equals(SMS_RECEIVED))
{
Bundle bundle = intent.getExtras();
if (bundle != null)
{
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus.length == 0)
{
return;
}
SmsMessage[] messages = new SmsMessage[pdus.length];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pdus.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sb.append(messages[i].getMessageBody());
}
String sender = messages[0].getOriginatingAddress();
String message = sb.toString();
if(message.equals("something"))
{
abortBroadcast();
}
}
Thanks in advance.
You May look at this-Intercept Incoming SMS Message and Modify it What you need to do is check when message is came from hangout application it it contains in address or subject that is diff from other message format.For message abort may look the abortBroadcast(); method inside broadcast message receiver in android that receives intent for incoming message.Hope this Helps you.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can we delete an SMS in Android before it reaches the inbox?
I've already gotten receive SMS code running successfully. What I cannot determine is a way (or if it's possible) to process messages sent from specific phone numbers within my app without them being made visible to the user. All other SMS's sent from other phone number would be handled by the normal Android SMS processing. I.e., SMS's from selected numbers should not be visible to the phone user and the rest should. Any suggestions?
Here's the SMSReceiver code (taken straight from Wei-Meng Lee's book):
public class SMSReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//get the received SMS message
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null) {
// retrieve the SMS message
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]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += "\nMessage Text:\n";
str += msgs[i].getMessageBody().toString();
str += "\nLength="+msgs[i].getMessageBody().toString().length()+"\n";
} // [END FOR]
// display the new SMS message
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
// send a broadcast intent to update the SMS received in the activity
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
} // [END IF]
} // [END onReceive]
} // [END SMSReceiver]
well if you put an if before you start concatenating str, you can check the originating address, and compare it to your blacklist of addresses, if its on the list, then simply use the continue to skip the concatenations
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Read all SMS from a particular sender
I want to know how to read sms and how split mobile number and message body. Please give me a sample code.
Code for the intent receiver that will read the SMS from intent received and show the message.
public class SmsReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
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();
}
}
}
And make sure to add this permission in your manifest file.
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
Also, msgs[i].getOriginatingAddress() gives you the sender of the SMS and you can check if this is your specific number or not. And then use msgs[i].getMessageBody().toString(); to show the body of the SMS.
This tutorial covers some of the aspects of your question.
Hope it helps.
I am developing a simple SMS Application in android,i am able to send and receive messages.
during receiving of sms i want my application should open whenever the sms comes from a specified number with some notification within the app and it shouldnot open whenever it comes from unspecified number..
what i am able to do is open the app whenever the message comes from the specified number but not able to stop my application from getting invoked(incase of unspecified number).
Help..
Your problem lies here:
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
phNum = msgs[i].getOriginatingAddress();
if("9716009159".equals(phNum)){
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
abortBroadcast();
}
else{
clearAbortBroadcast();
}
}
//---display the new SMS message---
//Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
//---launch the MainActivity--
Intent mainActivityIntent = new Intent(context, MainActivity.class);
mainActivityIntent.putExtra("ph", phNum);
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainActivityIntent);
//---send a broadcast to update the SMS received in the activity---
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
}
}
The launching of your activity should only happen when the message is from the number you specify. At the moment you launch the activity as long as the message is not null. Instead of the code above, use this:
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
phNum = msgs[i].getOriginatingAddress();
if("9716009159".equals(phNum)){
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();
//---launch the MainActivity--
Intent mainActivityIntent = new Intent(context, MainActivity.class);
mainActivityIntent.putExtra("ph", phNum);
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainActivityIntent);
//---send a broadcast to update the SMS received in the activity---
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
abortBroadcast();
}
else{
clearAbortBroadcast();
}
}
}
}
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
str += "SMS from " + msgs[i].getOriginatingAddress();// note this line gives your contact number
str += " ::::::::::::::::::::";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
}
This code what you are writing in your broad cast reciver
see // code which gives the incoming message number
get the number from there and check the number with the number which you want to restrict if true then dont start you application if false start your application thats it
If you need more assistance comment me
It's not a good idea to open your application when the SMS comes. Imagine your user playing a game or watching youtube, and when the SMS comes your application appears in the foreground without user expecting it. This is not pleasant for your users. Good practice is to implement a service, that will scan received SMS and check whether number is specified or not. Then if number is specified you can add a notification to the notification bar to make user see the alert. When user clicks on your notification he opens your app and sees the information you provide. Hope this helps you.
You need to handle the sms received event in a broadcastreceiver, not an activity. In the broadcastreceiver's onReceive method you can filter the phone numbers, and if it is a phone number you're interested in, you can launch your activity.
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
if(phone number matches your filter){
startActivity(your intent);
}
}
}
}
And to declare the receiver in the manifest:
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
EDIT: The phone number you get from the system probably contains country code, area code etc, and therefore you should compare the strings like this:
if(extractedNumber.contains(yourNumber)){
//do your stuff
}