arraylist is not updating correctly - android

I am using contentobserver to monitor SMS. It all works fine. When I try to save these SMS to a database, it shows an error error near "t" syntax error for a particular SMS. When I delete this particular SMS there is no problem. After installing, it shows all the messages correctly in order. But the error is sent to the end of my arraylist. Also the SMS sent from my phone after this are updated in between the list, not on the last position. Please help.
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,list);
setListAdapter(adapter);
data = Incoming_outgoing_smsActivity.this.openOrCreateDatabase("Messages", MODE_PRIVATE, null);
data.execSQL("CREATE TABLE IF NOT EXISTS recor(text varchar(300));");
Cursor cur = data.rawQuery("SELECT * FROM recor", null);
while(cur.moveToNext()) {
String content = cur.getString(cur.getColumnIndex("text"));
backward_list.add(content);
list.add(content);
}
adapter.notifyDataSetChanged();
Cursor cursor = getContentResolver().query(Uri.parse("content://sms"), null, null, null, null);
while(cursor.moveToNext()) {
String number = cursor.getString(cursor.getColumnIndex("address"));
String[] projection = new String[] {ContactsContract.PhoneLookup.DISPLAY_NAME};
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor cursor_name = getContentResolver().query(contactUri, projection, null, null, null);
String body = cursor.getString(cursor.getColumnIndex("body"));
String type = cursor.getString(cursor.getColumnIndex("type"));
long date1= cursor.getLong(cursor.getColumnIndex("date"));
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss.SSS");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(date1);
try {
int n = cursor.getInt(cursor.getColumnIndex("type"));
switch (n) {
case 1:
String message = "FROM "+number+"\n"+formatter.format(calendar.getTime())+"\n"+"Message:-"+body;
if(backward_list.contains(message)) {
continue;
} else {
list.add(message);
backward_list.add(message);
data.execSQL("INSERT INTO recor VALUES('"+message+"')");
}
break;
case 2:
String messag = "TO "+number+"\n"+formatter.format(calendar.getTime())+"\n"+"Message:-"+body;
if(backward_list.contains(messag)) {
continue;
} else {
list.add(messag);
backward_list.add(messag);
data.execSQL("INSERT INTO recor VALUES('"+messag+"')");
}
break;
default:
break;
}
}
catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
continue;
}
}
The above code saves the current SMS in your inbox to the database. The code below is used to update your inbox when a new SMS arrives. It does toast the arrived messages but doesn't insert them into the database.
data = Incoming_outgoing_smsActivity.this.openOrCreateDatabase("Messages", MODE_PRIVATE, null);
data.execSQL("CREATE TABLE IF NOT EXISTS recor(text varchar(300));");
super.onChange(selfChange);
Cursor cursor = getContentResolver().query(Uri.parse("content://sms"), null, null, null, null);
while(cursor.moveToNext()) {
String number = cursor.getString(cursor.getColumnIndex("address"));
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME};
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor cursor_name = getContentResolver().query(contactUri, projection, null, null, null);
if(cursor_name.moveToFirst()) {
name = cursor_name.getString(cursor_name.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
String body = cursor.getString(cursor.getColumnIndex("body"));
String type = cursor.getString(cursor.getColumnIndex("type"));
long date1= cursor.getLong(cursor.getColumnIndex("date"));
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss.SSS");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(date1);
int n = cursor.getInt(cursor.getColumnIndex("type"));
switch (n) {
case 1:
String message = "FROM "+number+"\n"+formatter.format(calendar.getTime())+"\n"+"Message:-"+body;
if(backward_list.contains(message)) {
continue;
} else {
list.add(message);
backward_list.add(message);
data.execSQL("INSERT INTO recor VALUES('"+message+"')");
}
break;
case 2:
String messag = "TO "+number+"\n"+formatter.format(calendar.getTime())+"\n"+"Message:-"+body;
if(backward_list.contains(messag)) {
continue;
} else {
list.add(messag);
backward_list.add(messag);
data.execSQL("INSERT INTO recor VALUES('"+messag+"')");
}
break;
default:
break;
}

At a guess there was some sort of restricted character/word in the one SMS.
You should use prepared statements to take care of the issue.
See this SO Answer for an example.
For your second issue about the order of display, change/use an ORDER BY in your query to set the proper order.

Related

How to fetch 50 by 50 contacts from contact list?

I want to fetch 50 by 50 contacts from the contact list.
I tried for loop but it gives me 10 contacts randomly.
How to fetch 50 by 50 contacts .. please help me
My code :
ArrayList<Contact_Model> contactList = new ArrayList<Contact_Model>();
Uri uri = ContactsContract.Contacts.CONTENT_URI;
Cursor contactsCursor = getContentResolver().query(uri, null, null,
null, ContactsContract.Contacts.DISPLAY_NAME + " ASC "); // Return
if (contactsCursor.moveToFirst()) {
do {
long contctId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));
Uri dataUri = ContactsContract.Data.CONTENT_URI; // URI to get
Cursor dataCursor = getContentResolver().query(dataUri, null,
ContactsContract.Data.CONTACT_ID + " = " + contctId,
null, null);
// Strings to get all details
String displayName = "";
String mobilePhone = "";
String contactNumbers = "";
String cNumber = "";
String contactImage = "";
String imnage = "";
Cursor phonesCursor = null;
Person.Urls urls;
try {
Uri phoneUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode("Phone number"));
phonesCursor = context.getContentResolver().query(phoneUri, new String[]{ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI}, null, null, null);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
if (dataCursor.moveToFirst()) {
try {
imnage = dataCursor.getString(1);
Log.e("detail", "==============" + imnage);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
if (dataCursor.moveToFirst()) {
displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));// get
do {
for (i = 0; i < 50; i++) {
if (dataCursor. getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
switch (dataCursor.getInt(dataCursor.getColumnIndex("data2"))) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
contactNumbers = dataCursor.getString(dataCursor.getColumnIndex("data1"));
contactNumbers += mobilePhone;
int id = 1;
if (String.valueOf(contactNumbers).charAt(0) == '+') {
if (contactNumbers.length() == 13) {
String trim_num = contactNumbers.substring(3);
cNumber = trim_num;
cNumber = cNumber.replaceAll(" ", "");
Log.d("number", cNumber);
}
} else {
cNumber = contactNumbers;
cNumber = cNumber.replaceAll(" ", "");
Log.d("without + number", cNumber);
}
// break;
}
}
}
break;
} while (dataCursor.moveToNext()); // Now move to next
if (!cNumber.equals("")) {
contact = new Contact();
contact.setContctId(String.valueOf(contctId));
contact.setContactNumber(cNumber);
contact.setContactName(displayName);
contact.setContactImg(imnage);
contact.save();
contactList.add(new Contact_Model(displayName, cNumber, imnage));// Finally add
} else {
Log.d("Contact : ", "Don't add empty contact");
}
}
} while (contactsCursor.moveToNext());
}
I also tried LIMIT 50 in cursor.
but when i use LIMIT no contacts available
Cursor contactsCursor = getContentResolver().query(uri,
null, null, null, "LIMIT 10, " + count);
count += 10;
Way 1
If you want pagination in query then use offset
final Cursor c = getContentResolver.query(uri,null,null,null," LIMIT 50 OFFSET 2");
Hold value of last offset and send ++offset the next time.
Way 2
You can get all contacts at once, and then set in list by using pagination.
#Related question.
Suggestion
I faced this problem also, when contacts are too much eg. 500-1000, then it takes some seconds to fetch.
I solved it by fetching contact list before that activity appear. I fetched contacts and hold them in Application class, and cleared after use (to remove memory leak).

Get Only Before 5 Minutes Inbox SMS

I want to select only latest inbox SMS on button click. Here is my code.
btnGet = (Button) findViewById(R.id.btnGet);
btnGet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (fetchInbox() != null) {
ArrayList sms1 = fetchInbox();
for (int i = 0; i < sms1.size(); i++) {
String st = sms1.get(i).toString();
String[] sArr = st.split("\\$");
mobile = sArr[0];
sms = sArr[1];
useGet(mobile, sms);
}
} else {
textView1.setText("no sms");
}
} catch (Exception ex) {
textView1.setText("Exception" + ex.getMessage());
}
}
});
And here is my function to fetch SMS.
public ArrayList fetchInbox()
{
ArrayList sms = new ArrayList();
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uriSms, new String[]{"_id", "address", "date", "body"},null,null,null);
cursor.moveToFirst();
while (cursor.moveToNext()) {
String id = cursor.getString(0);
String address = cursor.getString(1);
String body = cursor.getString(3);
sms.add(address + "$" + body + "$" + id);
}
return sms;
}
I am able get all inbox SMS by this code, but I want to select only before 5 minutes inbox SMS. I am new with android apps.
Cursor cursor = getContentResolver().query (Uri uri,
String[] projection,
String selection,
String[] selectionArgs,
String sortOrder)
selection : A filter declaring which rows to return. Passing null will
return all rows for the given URI.
As you can see, you can specifies the criteria for selecting rows.
First get the current date time.
Calendar date = Calendar.getInstance();
long t = date.getTimeInMillis();
Then subtract 5 mins from current time.
static final long ONE_MINUTE_IN_MILLIS = 60000;
Date afterSubtractingFiveMins = new Date(t - (5 * ONE_MINUTE_IN_MILLIS));
Now create the filter and query the messages.
String filter = "date>=" + afterSubtractingFiveMins.getTime();
Cursor cursor = getContentResolver().query(uriSms, new String[]{"_id", "address", "date", "body"},filter,null,null);
PS: I didn't check the code. You may have to optimize.

How can i get only all mobile numbers from android phone book?

That's my code , which give me all number of my android device like mobile numbers, whatsapp numbers and landline numbers. But i want only mobile number and landline numbers from android phone book into my application. How can i get only mobile number and landline number of every contact name?
private void addContactsInList() {
// TODO Auto-generated method stub
Cursor cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
try {
ContactsListClass.phoneList.clear();
} catch (Exception e) {
}
while (cursor.moveToNext()) {
String phoneName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int phoneId = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Contact cp = new Contact();
cp.setName(phoneName);
cp.setNumber(phoneNumber);
cp.setId(phoneId);
ContactsListClass.phoneList.add(cp);
// Log.e("add to list", "content" + phoneId +phoneName +phoneNumber);
}
cursor.close();
lv = new ListView(context);
lv.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
llContainer.addView(lv);
Collections.sort(ContactsListClass.phoneList, new Comparator<Contact>() {
#Override
public int compare(Contact lhs,
Contact rhs) {
return lhs.getName().compareTo(
rhs.getName());
}
});
contactAdapter = new ContactsAdapter(MainActivity.this,
ContactsListClass.phoneList);
lv.setAdapter(contactAdapter);
inside your while loop, put something like this:
while (phoneCursor.moveToNext()) {
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
//String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int type = phoneCursor.getInt(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
switch (type) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
// do something with the Home number here...
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
output.append("\n Phone number:" + phoneNumber);
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
// do something with the Work number here...
break;
}
}
as seen in this answer
Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
Uri phonesUri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY);
String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL}
Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null);
if(cursor != null && cursor.moveToFirst()){
do{
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int type = phoneCursor.getInt(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
switch (type) {
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
break;
}
}while(cursor.moveToNext());
}

Getting calendar events in android 4.0 and above

I am new to the android development. I am working on calender application where i need to add/delete and get all events in between the date range. I have add and delete the events successufully but problem is when i am getting the calender events in between the date range. I am getting the calender events but when i go to SPlanner i can see those events are not added in the calender as i have already deleted them. I do not know from where i am getting those events.Please suggest. Here is the code i have written to get the calender events:-
public void onGetEvent (final String fullCallbackName, String title,String startDate,String endDate) throws JSONException
{
try
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
final ArrayList<JSONObject> calEvents = new ArrayList();
if(calEvents.size() !=0)
{
calEvents.clear();
}
ContentResolver contentResolver = getContentResolver();
String selection = "((dtstart >= "+(dateFormat.parse(startDate).getTime())+") AND (dtend <= "+(dateFormat.parse(endDate).getTime())+"))";
Cursor cursor = contentResolver.query(Uri.parse(getCalendarUriBase() + "events"),
(new String[] { "_id", "title", "dtstart","dtend","eventLocation","description"}), selection, null, null);
Log.e("cursor.getCount before:","callbackFuncName:" + cursor.getCount());
while (cursor.moveToNext()) {
String _id = cursor.getString(0);
String displayName = cursor.getString(1);
Log.e("cursor.getCount before:","callbackFuncName:" + displayName);
String[] separated = displayName.split(":");
if(separated[0]!= null && title.equals(separated[0]))
{
JSONObject dictionary = new JSONObject();
String dstart = dateFormat.format(new Date(Long.parseLong(cursor.getString(2))));//cursor.getString(2);
String dEnd = dateFormat.format(new Date(Long.parseLong(cursor.getString(3))));//cursor.getString(3);
String eventlocation = cursor.getString(4);
String description = cursor.getString(5);
dictionary.put("identifier", _id);
dictionary.put("title", displayName);
dictionary.put("startDate", dstart);
dictionary.put("endDate", dEnd);
dictionary.put("venue", eventlocation);
dictionary.put("notes", description);
calEvents.add(dictionary);
}
}
if(fullCallbackName != null && !fullCallbackName.equals(""))
{
runOnUiThread(new Runnable() {
public void run()
{
webView.loadUrl("javascript:"+fullCallbackName+" ("+calEvents+")") ;
}
});
}
}
catch(Exception e)
{
Log.e("string", e.toString());
}
}
}
code for getting the calender DB is:-
private String getCalendarUriBase() {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor cursor = null;
try {
cursor = managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
// eat
}
if (cursor != null) {
calendarUriBase = "content://calendar/";
} else {
calendars = Uri.parse("content://com.android.calendar/calendars");
try {
cursor = managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
// eat
}
if (cursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}
Log.d("Sandeep",
calendarUriBase);
// managedCursor.close();
return calendarUriBase;
}
With your query you will see deleted events because they are still in the database (for being able to sync the deletion whenever the next sync is due). That's what the DELETED column is for.
To find all events between a start and end date use the Instances class of the CalendarContract API as in the code below. This code returns only visible events!
I've written a blog post about the CalendarContract content provider detailing this and other stuff.
long begin = // starting time in milliseconds; for you probably cursor.getLong(2)
long end = // ending time in milliseconds; cursor.getLong(3)
String[] proj =
new String[]{
Instances._ID,
Instances.TITLE,
Instances.BEGIN,
Instances.END,
Instances.EVENT_LOCATION,
Instances.DESCRIPTION,
Instances.EVENT_ID};
Cursor cursor =
Instances.query(getContentResolver(), proj, begin, end);
if (cursor.getCount() > 0) {
// do your JSON thing
}

MMS sender address problem

I am trying to get the address of the sender but I run into a little problem. If the person that sends the message is the first person in any of the conversations to send one, the query of the content://mms/inbox returns with zero rows?? but when someone sends any other mms message it will return back with the _id fine and i dont understand why the first one wont work right?
private String checkMmsMessages(Context context){
String address = "address";
Cursor curPdu = context.getContentResolver ().query(Uri.parse("content://mms/inbox"), null, null, null, null);
if(curPdu.moveToNext()){ //first MMS message curPdu.moveToNext() is false
String id = curPdu.getString (curPdu.getColumnIndex ("_id"));
Log.v("MMS", "ID1: " + id.toString());
Uri uriAddr = Uri.parse ("content://mms/" + id + "/addr");
Cursor curAddr = context.getContentResolver().query(uriAddr,null,"type=137",null,null);
if(curAddr.moveToNext()){
address = curAddr.getString (curAddr.getColumnIndex ("address"));
Log.v("MMS", "Address1: " + address.toString());
if(address.contentEquals("insert-address-token")){
Cursor curAddr2 = context.getContentResolver().query(uriAddr,null,"type=151", null,null);
if(curAddr2.moveToNext()){
address = curAddr2.getString(curAddr2.getColumnIndex("address"));
}
}
}
}
Log.v("MMS", address.toString());
return address;
}
Also something else that does not make sense is when when i have the phone plugged into the computer and step through the that section with the debugger, that problem does not happen and it gets the address every time.... it only happens when the phone is not connected, i just dont understand?
The problem was I was checking the database before the message was put into the database so I had to put a delay on the check
I think the issue is you are passing a value for selectionArgs instead of null to the query() method. I am not actually calling the mCursor's moveToNext() method in my code but instead I am implementing this logic in the getView() method of a SimpleCursorAdapter.
Uri uri = Uri.parse("content://mms-sms/conversations/" + mThreadId);
String[] projection = new String[] { "body", "person", "sub",
"subject", "retr_st", "type", "date", "ct_cls", "sub_cs",
"_id", "read", "ct_l", "st", "msg_box", "reply_path_present",
"m_cls", "read_status", "ct_t", "status", "retr_txt_cs",
"d_rpt", "error_code", "m_id", "date_sent", "m_type", "v",
"exp", "pri", "service_center", "address", "rr", "rpt_a",
"resp_txt", "locked", "resp_st", "m_size" };
String sortOrder = "normalized_date";
Cursor mCursor = getActivity().getContentResolver().query(uri,projection, null, null, sortOrder);
String messageAddress;
int type;
while (mCursor.moveToNext()) {
String messageId = mCursor.getString(mCursor.getColumnIndex("_id"));
Uri.Builder builder = Uri.parse("content://mms").buildUpon();
builder.appendPath(messageId).appendPath("addr");
Cursor c = mContext.getContentResolver().query(builder.build(), new String[] {
"*"
}, null, null, null);
while (c.moveToNext()) {
messageAddress = c.getString(c.getColumnIndex("address"));
if (!messageAddress.equals("insert-address-token")) {
type = c.getInt(c.getColumnIndex("type"));
c.moveToLast();
}
}
c.close();
}
try this...
private String getAddressNumber(String id) {
String selectionAdd = new String("msg_id=" + id);
String uriStr = MessageFormat.format("content://mms/{0}/addr", id);
Uri uriAddress = Uri.parse(uriStr);
Cursor cursor = getContentResolver().query(uriAddress, null, selectionAdd, null, null);
String phoneNum = null;
if (cursor.moveToFirst()) {
do {
String number = cursor.getString(cursor.getColumnIndex("address"));
if (number != null) {
boolean isNumberFormat = true;
try {
Long.parseLong(number.replace("-", ""));
phoneNum = number;
} catch (NumberFormatException e) { // ex) "insert-address-token"
// if (phoneNum == null) {
// phoneNum = number;
// }
isNumberFormat = false;
}
if (isNumberFormat)
break;
}
} while (cursor.moveToNext());
}
if (cursor != null) {
cursor.close();
}
return phoneNum;
}

Categories

Resources