Save Contacts as "Phone contact" - android

Is there way to programmatically adds the contact to the internal phone contacts book as a "phone contact"?
I've tried:
list.add(ContentProviderOperation
.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
with this parameters contact successfully saves to the phone, but in case i set up filter to "display phone contacts only" - created contact not appears. btw, i've read that contacts with null type can be loses drying accounts synchronization (haven't remember the full case)
then i tried to retrieve ACCOUNT_TYPE and ACCOUNT_NAME from existing phone contact and gets Phone and Local Phone Account strings, but when i tried to save contact with same parameters:
list.add(ContentProviderOperation
.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "Phone")
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "Local Phone Account")
.build());
the result was the same to the first case with null type.
is there any constants (or it should be another way) to save data like "phone contact"?

The "phone only" account is not fully supported by plain Android, it's usually added (or unlocked) by device makers.
Here are the ones I know of, feel free to add more in case you find more.
The format is <maker>: ACCOUNT_TYPE, ACCOUNT_NAME
samsung: "vnd.sec.contact.phone: "vnd.sec.contact.phone"
htc: "com.htc.android.pcsc: "pcsc"
sony: "com.sonyericsson.localcontacts: "Phone contacts"
lge: "com.lge.sync: "Phone"
lge (option 2): "com.lge.phone"
t-mobile: "vnd.tmobileus.contact.phone: "MobileLife Contacts"
huawei: "com.android.huawei.phone: "Phone"
lenovo: "Local Phone Account: "Phone"
xiaomi: "com.xiaomi"
oppo: "com.oppo.contacts.device"

Related

Android, Insert RawContact not showing on Sony Xperia

I'm using ContactsContract to insert RawContacts in my app. The following code (and showing the inserted contact in the contact app) works on all devices, but not on Sony Xperia (Android 4.4.4).
ContentValues p=new ContentValues();
p.put(ContactsContract.RawContacts.ACCOUNT_TYPE, getActivity().getPackageName());
p.put(ContactsContract.RawContacts.ACCOUNT_NAME,
DataHelper.getAppName(getActivity()));
Uri rowcontact = null;
long rawcontactid = 0;
try {
rowcontact = getActivity().getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, p);
rawcontactid = ContentUris.parseId(rowcontact);
Log.d(DEBUG_KEY, "CONTACT ADDED: " + rawcontactid);
}catch(Exception e){
Log.d(DEBUG_KEY, "CONTACT ADDED FAILED 1: " + e.getMessage());
return "";
}
On the Sony Xperia Device there is no error or exception. The console display the correct CONTACT-ADDED-ID. But in the android contact app the newly inserted (Raw)-Contact is not showing. I have enabled all groups, etc. in the filter-settings in the contact app.
Ok. Problem is solved. On Sony Xperia Devices you must
(1) Specify an existing (google) account
p.put(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google");
p.put(ContactsContract.RawContacts.ACCOUNT_NAME, "google_account_username_on_device");
or (2) don't specify any account type details
//p.put(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google");
//p.put(ContactsContract.RawContacts.ACCOUNT_NAME, "google_account_username_on_device");
On other devices like Motorola oder Google Nexus you can specify a non existing, custom Account, like:
p.put(ContactsContract.RawContacts.ACCOUNT_TYPE, "my.app.name");
p.put(ContactsContract.RawContacts.ACCOUNT_NAME, "APP NAME");

phone number in EditText

I am having a form wherein i get user details, in that i have field called phone number and Edit Text to put phone number.
i have feeling that it would be great if that edittext automatically get filled by user phone number.
is there any method which automatically fills user phone number with national code??
below code was posted on stackoverflow i think it might work in some instances can anyone help me for same?
private String getMyPhoneNumber() {
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}
i have feeling that it would be great if that edittext automatically get filled by user phone
number
To solve the above problem, your best bet is to use,
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number = tm.getLine1Number();
with the permission added of course
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
But the problem is that the documentation for getLine1Number() says this method will return null if the number is "unavailable", but it does not say when the number might be unavailable.
is there any method which automatically fills user phone number with national code??
To solve this issue, unfortunately there doesn't seem to be a direct way to get the user's current country code. But since country codes don't ever change, it is acceptable to hard code a lookup table in your application.
<?xml version='1.0' encoding='UTF-8'?>
<icc>
<AF>93</AF>
<AL>355</AL>
<DZ>213</DZ>
<AD>376</AD>
<AO>244</AO>
<AQ>672</AQ>
<AR>54</AR>
<AM>374</AM>
<AW>297</AW>
<AU>61</AU>
<AT>43</AT>
<AZ>994</AZ>
<BH>973</BH>
<BD>880</BD>
<BY>375</BY>
<BE>32</BE>
<BZ>501</BZ>
<BJ>229</BJ>
<BT>975</BT>
<BO>591</BO>
<BA>387</BA>
<BW>267</BW>
<BR>55</BR>
<BN>673</BN>
<BG>359</BG>
<BF>226</BF>
<MM>95</MM>
<BI>257</BI>
<KH>855</KH>
<CM>237</CM>
<CA>1</CA>
<CV>238</CV>
<CF>236</CF>
<TD>235</TD>
<CL>56</CL>
<CN>86</CN>
<CX>61</CX>
<CC>61</CC>
<CO>57</CO>
<KM>269</KM>
<CG>242</CG>
<CD>243</CD>
<CK>682</CK>
<CR>506</CR>
<HR>385</HR>
<CU>53</CU>
<CY>357</CY>
<CZ>420</CZ>
<DK>45</DK>
<DJ>253</DJ>
<TL>670</TL>
<EC>593</EC>
<EG>20</EG>
<SV>503</SV>
<GQ>240</GQ>
<ER>291</ER>
<EE>372</EE>
<ET>251</ET>
<FK>500</FK>
<FO>298</FO>
<FJ>679</FJ>
<FI>358</FI>
<FR>33</FR>
<PF>689</PF>
<GA>241</GA>
<GM>220</GM>
<GE>995</GE>
<DE>49</DE>
<GH>233</GH>
<GI>350</GI>
<GR>30</GR>
<GL>299</GL>
<GT>502</GT>
<GN>224</GN>
<GW>245</GW>
<GY>592</GY>
<HT>509</HT>
<HN>504</HN>
<HK>852</HK>
<HU>36</HU>
<IN>91</IN>
<ID>62</ID>
<IR>98</IR>
<IQ>964</IQ>
<IE>353</IE>
<IM>44</IM>
<IL>972</IL>
<IT>39</IT>
<CI>225</CI>
<JP>81</JP>
<JO>962</JO>
<KZ>7</KZ>
<KE>254</KE>
<KI>686</KI>
<KW>965</KW>
<KG>996</KG>
<LA>856</LA>
<LV>371</LV>
<LB>961</LB>
<LS>266</LS>
<LR>231</LR>
<LY>218</LY>
<LI>423</LI>
<LT>370</LT>
<LU>352</LU>
<MO>853</MO>
<MK>389</MK>
<MG>261</MG>
<MW>265</MW>
<MY>60</MY>
<MV>960</MV>
<ML>223</ML>
<MT>356</MT>
<MH>692</MH>
<MR>222</MR>
<MU>230</MU>
<YT>262</YT>
<MX>52</MX>
<FM>691</FM>
<MD>373</MD>
<MC>377</MC>
<MN>976</MN>
<ME>382</ME>
<MA>212</MA>
<MZ>258</MZ>
<NA>264</NA>
<NR>674</NR>
<NP>977</NP>
<NL>31</NL>
<AN>599</AN>
<NC>687</NC>
<NZ>64</NZ>
<NI>505</NI>
<NE>227</NE>
<NG>234</NG>
<NU>683</NU>
<KP>850</KP>
<NO>47</NO>
<OM>968</OM>
<PK>92</PK>
<PW>680</PW>
<PA>507</PA>
<PG>675</PG>
<PY>595</PY>
<PE>51</PE>
<PH>63</PH>
<PN>870</PN>
<PL>48</PL>
<PT>351</PT>
<PR>1</PR>
<QA>974</QA>
<RO>40</RO>
<RU>7</RU>
<RW>250</RW>
<BL>590</BL>
<WS>685</WS>
<SM>378</SM>
<ST>239</ST>
<SA>966</SA>
<SN>221</SN>
<RS>381</RS>
<SC>248</SC>
<SL>232</SL>
<SG>65</SG>
<SK>421</SK>
<SI>386</SI>
<SB>677</SB>
<SO>252</SO>
<ZA>27</ZA>
<KR>82</KR>
<ES>34</ES>
<LK>94</LK>
<SH>290</SH>
<PM>508</PM>
<SD>249</SD>
<SR>597</SR>
<SZ>268</SZ>
<SE>46</SE>
<CH>41</CH>
<SY>963</SY>
<TW>886</TW>
<TJ>992</TJ>
<TZ>255</TZ>
<TH>66</TH>
<TG>228</TG>
<TK>690</TK>
<TO>676</TO>
<TN>216</TN>
<TR>90</TR>
<TM>993</TM>
<TV>688</TV>
<AE>971</AE>
<UG>256</UG>
<GB>44</GB>
<UA>380</UA>
<UY>598</UY>
<US>1</US>
<UZ>998</UZ>
<VU>678</VU>
<VA>39</VA>
<VE>58</VE>
<VN>84</VN>
<WF>681</WF>
<YE>967</YE>
<ZM>260</ZM>
<ZW>263</ZW>
</icc>
Please set that phonenumber in your edittext like this edittext.setText(mTelephonyMgr.getLine1Number())
There is plenty of questions relating to this.
Your method relies on the phonenumber being stored on the sim, which it rarely is.
The most reliable method is to let users input the number themselves. Though it would not hurt to let the getLine1Number() method try and set the number first.
I have chosen to let my app send a SMS to the number with an unique message, which when received, completes the verification of the input number. The number then finally is stored in a sharedPreference.
This method of course runs short if the user changes sim but if you need to handle that, it could be done by also saving the IMEI so that it can be checked when app starts.

What is logtype=100 OR logtype=500?

When I use ContentResolver.query(Calls.CONTENT_URI, null, Calls._ID = 'call ID here', null,null); the text (logtype=100 OR logtype=500) is added to the query.
what do 100 and 500 mean ?
I suppose you have a Samsung Android device. Look here:
100 is for voice calls.
SMS are duplicated as Calls(Samsung galaxy S II)

How to add postal address to contacts in android programmaticaly?

I am developing app which add contact info to android contact list .to How to add postal address to contacts in android programmatically ?
It's been a while that this has been asked but maybe someone else is still interested in it. How to add a contact with address info:
import static android.provider.ContactsContract.Data;
import static android.provider.ContactsContract.Intents.Insert;
private void createContactIntent() {
Intent contactIntent = new Intent(ContactsContract.Intents.Insert.ACTION, ContactsContract.Contacts.CONTENT_URI);
contactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
contactIntent.putExtra(Insert.NAME, "Sergio Mendes");
contactIntent.putExtra(Insert.COMPANY, "Company One");
contactIntent.putExtra(Insert.POSTAL, "Street 1, 9999 City, Country");
contactIntent.putExtra(Data.IS_SUPER_PRIMARY, 1);
startActivity(contactIntent);
}
Note that some devices like Samsung S5 / A5 will put the whole address into the "street" field. If you have any optimizations for this let me know.
Postal address are stored like all the other info in the DATA table with a
MIMEtype == ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE
Please google ContactsContract.CommonDataKinds.StructuredPostalto find all the info.
If you need to know how to edit a contact in general I would suggest you to have a look to the SampleSyncAdapter in the Android SDK. It is a sync adapter so you don't need to study everything but updateContact in ContactManager is a good point to start with.

How to add/save Postal Address to Contacts in Android Application [duplicate]

I am developing app which add contact info to android contact list .to How to add postal address to contacts in android programmatically ?
It's been a while that this has been asked but maybe someone else is still interested in it. How to add a contact with address info:
import static android.provider.ContactsContract.Data;
import static android.provider.ContactsContract.Intents.Insert;
private void createContactIntent() {
Intent contactIntent = new Intent(ContactsContract.Intents.Insert.ACTION, ContactsContract.Contacts.CONTENT_URI);
contactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
contactIntent.putExtra(Insert.NAME, "Sergio Mendes");
contactIntent.putExtra(Insert.COMPANY, "Company One");
contactIntent.putExtra(Insert.POSTAL, "Street 1, 9999 City, Country");
contactIntent.putExtra(Data.IS_SUPER_PRIMARY, 1);
startActivity(contactIntent);
}
Note that some devices like Samsung S5 / A5 will put the whole address into the "street" field. If you have any optimizations for this let me know.
Postal address are stored like all the other info in the DATA table with a
MIMEtype == ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE
Please google ContactsContract.CommonDataKinds.StructuredPostalto find all the info.
If you need to know how to edit a contact in general I would suggest you to have a look to the SampleSyncAdapter in the Android SDK. It is a sync adapter so you don't need to study everything but updateContact in ContactManager is a good point to start with.

Categories

Resources