I'm doing an android application which does Backup & Restore Phone contacts into remote server as vcf file type.Suppose if I have 5 contacts, I could do Backup it into server well & good. After that If I delete 2 contacts in the mobile,now totally 3. But server has 5 contacts. Then If I restore from server, that 3 Contacts will gets duplicated. Below is my code. How could I avoid that duplication while restore contacts.
Code:
final MimeTypeMap mime = MimeTypeMap.getSingleton();
String tmptype = mime.getMimeTypeFromExtension("vcf");
final File file = new File(Environment.getExternalStorageDirectory().toString()+ "/contacts.vcf");
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(file), "text/x-vcard");
startActivity(i);
Add your contact in ArrayList < String> arrDupContact and while delete contact remove contact from arrDupContact also.While fetching contact from server check whether it contain that particular contact or not it is contain then bypass it, if not the add it.
I would you Set cont = new HashSet(), becasuse HashSet don`t allow duplicates by default.
Please find this quick summary here:
http://www.codejava.net/java-core/collections/java-collections-framework-summary-table
Please find a massive summary here:
https://docs.oracle.com/javase/8/docs/technotes/guides/collections/overview.html
Related
I created an app using phonegap to read and list all contact detail
to read contact here is the code
var field=["displayName"];
var options = new ContactFindOptions();
options.filter=""; // empty search string returns all contacts
options.multiple=true;
var contat=navigator.contacts.find(field, contactSucc, contactError,options);
function contactSucc(contacts) {
alert("loading contact");
var str="";
for (var i=0; i<contacts.length; i++) {
if(contacts[i].displayName)
str="Name = " +(contacts[i].displayName) + " <br/>";
$('#contactId').append(str);
}
}
This code work almost instant on android 2.3/4.1 (1 second) but it take too long on android 4.3 (25 second)
Most time is take by quering database
Please note:- all device loaded with same amount of contact info (around 1k contact detail)
Is there any workaround.. Or is there anyone who face the same problem
You can follow below steps:
Ask for the required information using 'desiredFields' filter.
Identify whether you want all contacts or contacts which has at least one contact number
Store fetched contacts in sql lite
Search new or updated contacts using date time stored when you last time fetched the contacts.
Based on "Modifying Contacts Using Intents" (Android developer documentation) and the answer to "Start add new contact activity and pass structured data", I've devised a way of populating the Android contact picker with data to be edited and saved by the user.
I also have a comparable "headless" method using ContentProviderOperation, which works as intended for all contact fields I've tried (name, phone number, e-mail, structured address etc.).
However, when using the intent, somehow the structured address gets discarded and does not appear in the contact picker. A website URL added with the same method works fine.
This is my code, somewhat condensed:
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
ArrayList<ContentValues> data = new ArrayList<ContentValues>();
ContentValues addressRow = new ContentValues();
addressRow.put(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
// The string values are prepared earlier, and I check that none of them is null.
// The ContentProviderOperation way doesn't seem to mind nulls, however.
addressRow.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, street);
addressRow.put(ContactsContract.CommonDataKinds.StructuredPostal.POBOX, pobox);
addressRow.put(ContactsContract.CommonDataKinds.StructuredPostal.NEIGHBORHOOD, neighborhood);
addressRow.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, city);
addressRow.put(ContactsContract.CommonDataKinds.StructuredPostal.REGION, region);
addressRow.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, postcode);
addressRow.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, country);
addressRow.put(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, addressType);
data.add(addressRow);
// This website address is shown in the contact edit screen, as expected:
ContentValues urlRow = new ContentValues();
urlRow.put(Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
urlRow.put(ContactsContract.CommonDataKinds.Website.URL, urlValue);
data.add(urlRow);
intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);
startActivityForResult(intent, ADD_CONTACT_REQUEST); // my own request code
Please note that using an intent and using a ContentProviderOperation are different ways of achieving the same thing, both appropriate for their respective scenarios. My question is about the intent, so the answers to the following SO questions were not too helpful:
"Not able to insert address of contacts in android"
"How to add structured data to a new contact Intent"
Any suggestions as to what I'm messing up here?
I am sure there is an easy solution to this one but I figured I would check with all the folks here first.
I am working on a database creation and management application for android where the user creates and manages data along the line of what PHPMyAdmin does for regular computers.
I have the section where the user creates a database and can insert tables with the appropriate styled data into the system.
The next priority is selecting which DB to enter and modify its contents. Is there a way to display the available databases, along with its table contents, in the form of a list-view for the user to enter and edit the desired data??
I know that this is a rather dull question, but this is basically the last piece of the puzzle for me to fit into this app before it is operational in a raw format. If you need any further information, or any code to examine, I will be happy to provide.
Thanks again for everyone's assistance.
Here's a good tutorial on SQLite databases and displaying contents in a ListView:
http://www.vogella.com/articles/AndroidSQLite/article.html#databasetutorial
It doesn't go over editing that much, but it's easy to see where he puts the values into the database.
thenewboston on YouTube is a good resource for Android tutorials and he goes over SQLite databases:
http://www.youtube.com/watch?v=gEg9OdufXmM
It's pretty comprehensive and slow if you already kinda know what you're doing so here is where he goes over inserting data/editing the database if you just wanna jump to that:
http://www.youtube.com/watch?v=S3Z4e7KgNdU
I know this below can be optimized but for now just create such a method to do it automatically. The method...
Creates an empty database of a random name for a second,
Saves the new database's location - getDatabasePath,
Quikly deletes the empty database,
Removes filename form the saved path to get the directory path olny,
List all files in the database path excluding '-journal' files.
And it goes like this:
ArrayList<String> arr_list_of_db_files = getDBFILES();
pivate ArrayList<String> getDBFILES()
{
ArrayList<String> arr = new ArrayList<String>;
String db_path, rand_name, str_tmp;
//ad.1-2. random file name for db
rand_name = new Random().nextInt((4000000-2000+1)+2000).toString()+".db";
db_path = openOrCreateDatabase(rand_name, MODE_PRIVATE, null).getPath();
//ad.3.
deleteDatabase(rand_name);
//ad.4.
db_path = db_path.replace("/" + rand_name, "");
//ad.5.
File [] files = new File(db_path).listFiles();
if (files == null) { return null; }
//so now we get the filenames one by one
for (int i = 0; i < files.length; i++)
{
str_tmp = files[i].getName();
if (!str_tmp.endsWith("-journal"))
{ arr.add(str_tmp); }
}
return arr;
}
Btw, I cant test the code, but I hope it is fine and some of you find it useful.
Edited: I have optimized the above code.
Now that PhoneGap is version 2.0, is there a (potentially undocumented) way to have a contact picker?
The docs make it seem like I'd have to write my own in JavaScript by requesting ALL the user's contacts, then building my own in-app contact picker.
http://docs.phonegap.com/en/2.0.0/cordova_contacts_contacts.md.html#Contacts
I've found a one-off plug-in for Android, but that's not helpful if there's no plug-in for iPhone, cause then I'd still have to write my own. I'm looking for a device agnostic method that says "let the user go pick a contact, then send them back here with that contact info"
I don't know whether you can use this solution for Android as well but for iPhone you can use the .chooseContact() method.
Example:
Choose a contact
function contactChooser(){
//The chooseContact method will open a new window with all you contacts
navigator.contacts.chooseContact(
//After picking a name you will receive the id of the chosen contact
function(id){
//In an options variable you can set some filter parameters
//In this example we will use the Id to receive the data of the chosen contact
var options = {
filter: ""+id
}
//In the fields variable we're going to set the fields we want to receive
//'*' = every data. More field values are explained
// here: http://bit.ly/T8YyuE
var fields = ['*'];
navigator.contacts.find(fields, onPickContactSuccess, onPickContactError, options);
}, null);
}
function onPickContactSuccess(contacts){
//contacts contains all data you've requested
var _name = contacts[0].name
alert('Last: '+_name.familyName+' First: '+_name.givenName);
}
I have copied contacts local phonebook and have shown them in my custom list. While copying the contact list i have maintained each contact and his detail in an array.
Now I want to open up the default details screen when i click on any contact in my list. Please tell me how is this possible.
Looking at the Intent.ACTION_VIEW Pass this action with a valid URI.
For anyone whos looking for specifics:
Intent contViewIntent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI,phn_user.getId());
Where getId return Id in String.