Is it possible to add a contact on the phone via code?

Asked

Viewed 3,577 times

2

In a list of users on a mobile system it is possible to have a button that adds the contact on mobile ?

Would be a button ADD that, when clicked, appeared that Android contact add screen, for example, with the name of the person and the number already filled in the fields.

I found the TAG:

<a href="tel: 19 99999999"> 19 999999 </a>

So the person chooses what to do, call, send SMS or add in the contacts.

Similar to the mailto:.

But specifically for what I want I haven’t found it yet.

  • Probably not, it’s something that needs to be created just like Whatsapp did.

  • 1

    In fact the mailto: adds nowhere, what he does is similar to tel: it calls the communication app, on tel: makes a call and mailto: calls the compose e-mail.

1 answer

1


You’re trying to access something that’s inside the phone from the browser. Note, it is the same thing as trying to access a resource from your pc by Chrome, for example, without a "face" there to give you this information I do not see many options. More vc can do a search to see if any browser provides this "face" that makes the glue between js and mobile.

Below is an example of how it would be using java:

 String DisplayName = "XYZ";
 String MobileNumber = "123456";
 String HomeNumber = "1111";
 String WorkNumber = "2222";
 String emailID = "[email protected]";
 String company = "bad";
 String jobTitle = "abcd";

 ArrayList < ContentProviderOperation > ops = new ArrayList < ContentProviderOperation > ();

 ops.add(ContentProviderOperation.newInsert(
 ContactsContract.RawContacts.CONTENT_URI)
     .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
     .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
     .build());

 //------------------------------------------------------ Names
 if (DisplayName != null) {
     ops.add(ContentProviderOperation.newInsert(
     ContactsContract.Data.CONTENT_URI)
         .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
         .withValue(ContactsContract.Data.MIMETYPE,
     ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
         .withValue(
     ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
     DisplayName).build());
 }

 //------------------------------------------------------ Mobile Number                     
 if (MobileNumber != null) {
     ops.add(ContentProviderOperation.
     newInsert(ContactsContract.Data.CONTENT_URI)
         .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
         .withValue(ContactsContract.Data.MIMETYPE,
     ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
         .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, MobileNumber)
         .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
     ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
         .build());
 }

 //------------------------------------------------------ Home Numbers
 if (HomeNumber != null) {
     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
         .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
         .withValue(ContactsContract.Data.MIMETYPE,
     ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
         .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, HomeNumber)
         .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
     ContactsContract.CommonDataKinds.Phone.TYPE_HOME)
         .build());
 }

 //------------------------------------------------------ Work Numbers
 if (WorkNumber != null) {
     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
         .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
         .withValue(ContactsContract.Data.MIMETYPE,
     ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
         .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, WorkNumber)
         .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
     ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
         .build());
 }

 //------------------------------------------------------ Email
 if (emailID != null) {
     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
         .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
         .withValue(ContactsContract.Data.MIMETYPE,
     ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
         .withValue(ContactsContract.CommonDataKinds.Email.DATA, emailID)
         .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
         .build());
 }

 //------------------------------------------------------ Organization
 if (!company.equals("") && !jobTitle.equals("")) {
     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
         .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
         .withValue(ContactsContract.Data.MIMETYPE,
     ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
         .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, company)
         .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
         .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, jobTitle)
         .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
         .build());
 }

 // Asking the Contact provider to create a new contact                 
 try {
     getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
 } catch (Exception e) {
     e.printStackTrace();
     Toast.makeText(myContext, "Exception: " + e.getMessage(), Toast.LENGTH_SHORT).show();
 } 
  • Beauty. And where I implement this (which I don’t even know, I can’t imagine) in my HTML ?

  • I see you’re beginner with android right? Here is a tutorial on how to list contacts: http:/examples.javacodegeeks.com/android/core/provider/android-contacts-example/. You will need to create a new project, create an interface (similar to html) and then merge the two things together.

  • No, I’m not a beginner on Android. I never started. My system is in HTML5. I just want to know if there is a command, a tag that allows me to click and add the contact. The closest I found was the tel:.

  • You know something about St?

  • I don’t know what Rest is. Can you open my mind, please ?

  • Come on, you are accessing an HTML page by mobile right? In this case you will need to get inside the phone where it runs java right? In this case you can create a Rest application and run a javascript by the mobile browser to then access the java layer (where the contacts are). Here is an example of the Rest application: http://www.k19.com.br/artigos/criando-um-webservice-restful-em-java/

  • Got it. Thanks. I got the logical sequence. Just as soon as it’s possible to do ?

  • Well, the point is, as I said, you’re trying to access something that’s inside the phone from the browser. Note, it is the same thing as trying to access a resource from your pc by Chrome, for example, without a "face" there to give you this information I do not see many options. More vc can do a search to see if any browser provides this "face" that makes the glue between js and mobile

  • 2

    The question is using html tag and link, so the answer should present something close to this and if it is not possible, it should be described where your code should go and what technology it uses, for example: "With html is not possible, but if it is Android, you can use java etc etc". I recommend you improve the friendly response

  • The answer was changed and yet I was negative. Do you want me to improve even more? I need to be more specific?

  • It was not me who negatived, unfortunately will have to wait for the users who negatively pass here, maybe they have not seen the issue, I will give you a positive vote, but a doubt, what is "dude"?

  • 2

    @Guilhermenascimento the "face" was a generic way to call the service on the cell side that will expose an interface to the client side consuming the information. I understand that using generic terms may not be ideal. However, putting complex responses to a user who does not know much about such technology can discourage them from pursuing development due to its complexity. In this topic would certainly help you explaining what this "little guy" would be, more before, he needs to understand the basics of mobile operation

  • I still think it’s better to use names that can be searchable after AP knows or not the technologies. In this case it is a protocol that calls an application, similar to a Intent, then the term may be application or instead of saying "see if any browser provides this little guy" it would be nice to say "see if any browser supports this protocol"

  • The question would not be a protocol, but a communication interface that provides internal data from the OS. More finally, after all everyone has the same idea, help and be helped. Hugs and good week

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.