Recognize/React on incoming SMS

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Postby plusminus » Fri Oct 03, 2008 8:10 pm

Snapper67 wrote:
Artur79 wrote:After sending SMS via Emulator I had following return in LogCat so works but there are still some errors.


Need to remove the line
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.abortBroadcast();
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


This is a normal broadcast and can not be aborted.


Yes, since sdk ??? sms cannot be aborted.

Thanks for reminding.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby Artur79 » Sun Oct 05, 2008 12:37 pm

now no errors, cheers :)
there's another small issue, it doesnt matter what I'll enter in Toast last param, which is for display length. It always appears for about one second only.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
Artur79
Developer
Developer
 
Posts: 26
Joined: Fri Sep 26, 2008 12:53 pm

Postby ninor » Mon Oct 06, 2008 9:01 am

I used that line in a new project, and it shows the toast for:
SHORT: displays the toast for 2 seconds.
LONG: 4 seconds
Image AndDev: Your Android Development Community / Tutorials | Here's my Basic ToolKit
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Postby rmeph » Sat Oct 11, 2008 4:29 pm

plz tel me if incoming sms block in new sdk(1.0).
rmeph
Senior Developer
Senior Developer
 
Posts: 121
Joined: Mon Dec 10, 2007 1:54 pm
Location: India

Sending SMS between emulators (SDK 1.0)

Postby diki » Sun Nov 23, 2008 7:36 am

Hi PlusMinus,

That was a nice tutorial for receiving SMS. As far as sending sms, when i try sending sms from one emulator instance to another (5554 to 5556), it doesn't work. Is there any restriction that both emulator instances must not be of the same project which has both SMS sender and receiver?

I also tried sending sms to a plain receiver in another project, but it dint help me..
The same code works when i send sms from eclipse DDMS view..

I think i am missing something very small..Can you pls help me?

Thanks in advance.
diki
Freshman
Freshman
 
Posts: 2
Joined: Sun Nov 23, 2008 6:45 am

Native SMS application

Postby faiz.india » Mon Nov 24, 2008 5:14 am

Hi All,
i have a query with respect to the native SMS application, i am able to send a sms using the native sms application but there is one more thing that i want to achieve in over here which is i want to make a log entry into my database if the sms send by the native SMS application succeeds. Can anyone give me a solution for the same.

I have used an intent to start the native SMS application which would return me a result but it send me the RESELT_CANCELLED result if even when the SMS sending succeeds or fails.


Thanks and Regards,
Faiz.
faiz.india
Developer
Developer
 
Posts: 32
Joined: Mon Jul 28, 2008 12:21 pm
Location: Pune - India

Top

DATA SMS to application port

Postby Astrlz » Thu Dec 18, 2008 2:33 pm

Hi,

I am currently trying to send SMS to a particular port of application
in Android Emulator. I am using the function sendDataMessage function
of SMSManager class. The SMS apparently never gets to the required
port of the application or the Inbox of the phone (Although the Text-
based Messages are received properly) . I am using the following
permissions on the receiver side of the application (found on one of
the threads of this group):
<receiver android:name=".YourBroadcastReceiver">
<intent-filter>
<action
android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms"/>
<data android:host="localhost"/>
<data android:port="your port"/>
</intent-filter>
</receiver>

Can anyone please help me out with alternative or correct me for
possible errors.

Thanking in advance...

Regards,

Astrlz


Snapper67 wrote:
Artur79 wrote:After sending SMS via Emulator I had following return in LogCat so works but there are still some errors.


Need to remove the line
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.abortBroadcast();
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


This is a normal broadcast and can not be aborted.
Astrlz
Freshman
Freshman
 
Posts: 4
Joined: Fri Dec 12, 2008 8:57 am

Postby nas061000 » Thu Dec 18, 2008 7:57 pm

android.provider.Telephony.SMS_RECEIVED

How did you find this intent? I've looked within AndroidManifest, and the Intent class and cannot find this referenced there...
nas061000
Junior Developer
Junior Developer
 
Posts: 13
Joined: Sat Dec 13, 2008 11:00 pm

Postby Astrlz » Tue Dec 23, 2008 8:09 am

Hi,

goto ur Manifest.xml file. define <action ...> tag inside ur xml and u will find that tag.

Regards,

Astrlz


nas061000 wrote:android.provider.Telephony.SMS_RECEIVED

How did you find this intent? I've looked within AndroidManifest, and the Intent class and cannot find this referenced there...
Astrlz
Freshman
Freshman
 
Posts: 4
Joined: Fri Dec 12, 2008 8:57 am

Postby Phlogiston » Thu Dec 25, 2008 9:48 pm

nas061000 wrote:android.provider.Telephony.SMS_RECEIVED

How did you find this intent? I've looked within AndroidManifest, and the Intent class and cannot find this referenced there...


I have a similar question: How did you know of this intent? Which reference did you use?
Phlogiston
Freshman
Freshman
 
Posts: 4
Joined: Thu Dec 25, 2008 8:56 pm

Trying to send multipart SMS.

Postby shchethan » Thu Jan 29, 2009 9:14 am

Hi,

I am trying to send a multipart SMS using the following code. While the SMS is being split into multiple parts and sent across, in the receiver, I am seeing some junk characters (as if i am reading a binary file). Obviously, I am not doing something correct. Can someone (plusminus ) help?

// SMS SENDER

package com.test;

import java.util.ArrayList;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.util.Log;

public class SMSSender extends Activity {



@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
SmsManager smsManager = SmsManager.getDefault();
String destAddr = "5556", scAddr = null, mMessageText =
"This is a sample SMS which has more than 160 characters if it has more characters, it will give an error as the standard SMS can contain only 160 characters. This text does not appear";
PendingIntent sentIntent = null, deliveryIntent = null;
try {
ArrayList<PendingIntent> listOfIntents = new ArrayList<PendingIntent>(0);
//PendingIntent il = PendingIntent.getBroadcast(this, 0, new Intent(), 0);
ArrayList<String> messages = smsManager.divideMessage(mMessageText);

Log.v("****************", messages.toString());

for (int i=0; i < messages.size(); i++){

PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(), 0);
listOfIntents.add(pi);
}
smsManager.sendMultipartTextMessage(destAddr, null, messages, listOfIntents, null);

} catch (Exception e) { Log.i("TEST", e.toString()); } } }


// SMS Receiver
package com.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.util.Log;


public class SMSReceiver extends BroadcastReceiver {
private static final String TAG = "SMSReceiver";
static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION)) {
StringBuilder buf = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdusObj.length];
for (int i = 0; i<pdusObj.length; i++) {
messages[i] = SmsMessage.createFromPdu ((byte[]) pdusObj[i]);
SmsMessage msg = messages[0];
Log.i(TAG, msg.getDisplayMessageBody());
}}}}}
shchethan
Freshman
Freshman
 
Posts: 3
Joined: Mon Jan 12, 2009 4:42 pm

How delete SMS programatically.

Postby Gulfam Hassan » Tue Mar 17, 2009 3:52 pm

Hi Plusminus,

I have received SMS successfully.there is confidential data in that SMS after getting the data i want to delete this SMS programatically How I can delete it?any one can help me any code snippet.

Regards,
Gulfam
Gulfam Hassan
Freshman
Freshman
 
Posts: 4
Joined: Wed Feb 18, 2009 1:24 pm
Location: Pakistan

Postby Gulfam Hassan » Fri Mar 20, 2009 5:35 pm

Hi,
I have received SMS get username and and password from sms and save them in preferences successfully with the below mentioned code.
There is confidentional data in it I don’t want to show this sms to the user in default INBOX. How I can delete this particular SMS at interception time OR how to stop this sms to go in the default INBOX OR discard this sms.
hope you understand my problem if not please reply me.
every thing is working fine up to received sms and save user credentials.


Received message syntax is. 12345 gulfam gulfam
username =gulfam
password = gulfam

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class MySMSReceiver extends BroadcastReceiver {
  3.  
  4.        
  5.  
  6.     private String userName = null;
  7.  
  8.     private String password = null;
  9.  
  10.     private String receivedSMS = null;
  11.  
  12.         @Override
  13.  
  14.         public void onReceive(Context context, Intent intent) {
  15.  
  16.                        
  17.  
  18.                 if(!intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
  19.  
  20.                         {
  21.  
  22.                                 return;
  23.  
  24.                         }
  25.  
  26.                         SmsMessage smsMsg[] = getMessagesFromIntent(intent);
  27.  
  28.                         for(int i=0; i < smsMsg.length; i++)
  29.  
  30.                         {
  31.  
  32.                                 receivedSMS = smsMsg[i].getDisplayMessageBody();
  33.  
  34.                                 if(receivedSMS.startsWith("12345"))
  35.  
  36.                                   {
  37.  
  38.                                 String[] dataArray = VOPUtility.breakIntoLines(receivedSMS, ' ');//break string into lines
  39.  
  40.                                           String uName = dataArray[1];//username = gulfam
  41.  
  42.                                           String uPassword = dataArray[2]; //password = gulfam
  43.  
  44.                                          
  45.  
  46.                                            this.updateUsernamePassword(context, uName, uPassword);
  47.  
  48.                                          
  49.  
  50.                                            Toast.makeText(context,"Your Settings Saved", Toast.LENGTH_LONG).show();
  51.  
  52.                                   }
  53.  
  54.                                                                
  55.  
  56.                     }
  57.  
  58.                        
  59.  
  60.            }
  61.  
  62.        
  63.  
  64.         private SmsMessage[] getMessagesFromIntent(Intent intent)
  65.  
  66.         {
  67.  
  68.                 SmsMessage receivedSMS[] = null;
  69.  
  70.                 Bundle bundle = intent.getExtras();
  71.  
  72.                 try{
  73.  
  74.                         Object pdus[] = (Object [])bundle.get("pdus");
  75.  
  76.                         receivedSMS = new SmsMessage[pdus.length];
  77.  
  78.                         for(int n=0; n < pdus.length; n++)
  79.  
  80.                         {
  81.  
  82.                                 byte[] byteData = (byte[])pdus[n];
  83.  
  84.                                 receivedSMS[n] = SmsMessage.createFromPdu(byteData);
  85.  
  86.                         }
  87.  
  88.                        
  89.  
  90.                         }catch(Exception e)
  91.  
  92.                 {
  93.  
  94.                         Log.e("GetMessages", "fail", e);
  95.  
  96.                 }
  97.  
  98.                 return receivedSMS;
  99.  
  100.         }
  101.  
  102.        
  103.  
  104.        
  105.  
  106.        
  107.  
  108.         //Here i am saving credentials in prefrences//
  109.  
  110.   public void updateUsernamePassword(Context context,String updatedUserName,String updatedPassword)
  111.  
  112.         {
  113.  
  114.                 userName= context.getString(R.string.username_key);
  115.  
  116.                 password = context.getString(R.string.password_key);
  117.  
  118.                  
  119.  
  120.                 PreferenceManager.getDefaultSharedPreferences(context).edit().putString(userName,updatedUserName).commit();
  121.  
  122.                 PreferenceManager.getDefaultSharedPreferences(context).edit().putString(password,updatedPassword).commit();
  123.  
  124.                
  125.  
  126.                                
  127.  
  128.  }
  129.  
  130. }
  131.  
  132.  
  133.  
  134.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4

I don't want to show this sms to user at any cost. is it possible if yes then how?
1: Delete it at interception time??
2: Stop it show user in default INBOX??
3: Discard it at time of interception??
4: any other way???
Help me ragarding this.

Best Regards,
Gulfam
Gulfam Hassan
Freshman
Freshman
 
Posts: 4
Joined: Wed Feb 18, 2009 1:24 pm
Location: Pakistan

Where can i put tje intent code

Postby DoM » Mon Mar 23, 2009 11:43 am

Hi,I want send and receive an sms..I wrote this code and it works perfectly...

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class SMS extends Activity
{
Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
txtMessage = (EditText) findViewById(R.id.txtMessage);

btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String phoneNo = txtPhoneNo.getText().toString();
String message = txtMessage.getText().toString();
if (phoneNo.length()>0 && message.length()>0)
sendSMS(phoneNo, message);

else
Toast.makeText(getBaseContext(),
"Inserisci numero e messsaggio.",
Toast.LENGTH_SHORT).show();
}
});

}


//---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, SMS.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);

}
}


SMSReceiver

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;

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 Ricevuto da " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}

}
}

Now I want send the sms using the Intent...Where I have to put the code??
Please help me..
DoM
Freshman
Freshman
 
Posts: 9
Joined: Mon Mar 16, 2009 12:43 pm

delete sms

Postby rahulp » Tue Apr 07, 2009 8:08 am

can anyone show how to delete an sms from the inbox progamatically??
rahul phadnis
rahulp
Developer
Developer
 
Posts: 30
Joined: Thu Feb 19, 2009 12:52 pm

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 10 guests