Reputation: 355
sir, i have a problem in updating my database. i've made a listview containing this data
name
phone number
status
i would like to update the status of person if he sent a message with a keyword "available" in it and would update my listview like this
name
phone number
available
so i decided to make the phone number as the trigger. if the phone number of the person who sent the message is in my listview, it will update the database. but here is my problem, if i saved the phone number in my listview in this format
09211234567
the sender will return their phone number as
+639211234567
so i worked around this by getting the substring and cut the phone number into "9211234567", and then add 0 to transform it into "09211234567".
however, the database status still doesn't update. but when i used the same technique in sending sms from emulator to emulator, it works just fine.
i saved the number of emulator in my listview as
5556
but the emulator returns
15555215556
so i just get the substring to get 5556
please help me. here is my code:
public static String sender;
public GroupDb info;
public String aStatus = "available";
public String nStatus = "not available";
public String addNum = "0";
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
info = new GroupDb(context);
Bundle bundle = intent.getExtras();
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]);
sender = messages[i].getOriginatingAddress();
}
for (SmsMessage msg : messages) {
if (msg.getMessageBody().contains("available")) {
info.open();
String remFirstChar = sender.substring(3);
addNum += remFirstChar;
Toast.makeText(context.getApplicationContext(), "received sms from: " +addNum,
Toast.LENGTH_LONG).show();
//if starts with +639
if(sender.length() == 13)
{
info.updateStatus(addNum, aStatus);
Toast.makeText(context.getApplicationContext(), "addNum: " +addNum,
Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(context.getApplicationContext(), "sender: " +sender,
Toast.LENGTH_LONG).show();
info.updateStatus(remFirstChar, aStatus);
}
info.close();
}//end if - available
here is how i updated my status
//update status
public void updateStatus(String mNumber, String mStatus) throws SQLException
{
// TODO Auto-generated method stub
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_STATUS, mStatus);
ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NUMBER + "=" + mNumber, null);
}
update: i even tried to enter "+63" format in listview but still, i won't update. all functions such as deletion and editing also don't work and shows force close.
Upvotes: 0
Views: 127
Reputation: 27659
You need to use PhoneNumberUtils
class.
Before saving the Phone Number in database first convert it as
String formattedNumber = PhoneNumberUtils.formatNumber(phonenumber);
For comparison you can use PhoneNumberUtils.compare
Upvotes: 2