I am trying to implement signup activity in android using phone number as done by whatsapp and many other applications these days. But i also need to know the name of the user but without prompting the user to enter it himself/herself. is it possible? how does whatsApp manages to do it?
Looking for any suggestions possible here. Thanks.
You can use ContactsContract.Profile
String name = "";
Cursor c = getApplication().getContentResolver().query(ContactsContract.Profile.CONTENT_URI, null, null, null, null);
if(c.moveToFirst()) {
name = c.getString(c.getColumnIndex("display_name"));
c.close();
}
You need permissions too
<uses-permission android:name="android.permission.READ_PROFILE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Read this Get Owner Name of an Android Device
Related
I want to know if there any method to check if my Skype client is sign in or sign out inside my Andriod Application. Actually I'm using Skype Uri to perform my App functionality.
Anyone can help me. Thanks !
It's not perfect, but what I'm doing is to check the number of Skype contacts in the Android Contacts DB. With later version of Skype (I'm using 4.7.0.45315) when you log out it clears that user's Skype contacts.
final String skypeTestCallID = "echo123";
ContentResolver cr = getContentResolver();
Cursor skypeContactsCursor = cr.query(ContactsContract.Data.CONTENT_URI,
null, "mimetype = 'vnd.android.cursor.item/com.skype.android.videocall.action' "
+ "and data_sync1 != '" + skypeTestCallID + "'", null, null);
boolean isLoggedInToSkype = skypeContactsCursor.getCount() == 0;
<etc>
The obvious flaw is that you can't differentiate between being 'not logged in' and 'logged in, with no contacts in your account', but for most scenarios it's a close enough approximation.
If anyone has a better approach I'd be glad to hear about it.
I want to read Contacts via Contacts Picker like this:
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(contact, CONTACT_PICK_CODE);
If I get the Result, the intent.getData() contains an uri to lookup the Contact, but I need the permission READ_CONTACTS to to read it.
I thought it may be possible to recieve a Contact without this permission, similar to the CALL permission: If I want to make a call directly, I need it, but without it I can send a number to the phone app, and the user must click on the call button. Is there a similar functionallity for READ_CONTACTS I'm not aware of?
You can retrieve Contact info without permissions and is something like you tell in the question.
In resume, you create an intent to pick a contact, this give you a URI (and temporally, also give you permissions to read it), then you use the URI to query to retrieve the data using Contact Provider API.
You can read more about it in Intents guide.
For example (from the guide):
static final int REQUEST_SELECT_PHONE_NUMBER = 1;
public void selectContact() {
// Start an activity for the user to pick a phone number from contacts
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
// Get the URI and query the content provider for the phone number
Uri contactUri = data.getData();
String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};
Cursor cursor = getContentResolver().query(contactUri, projection,
null, null, null);
// If the cursor returned is valid, get the phone number
if (cursor != null && cursor.moveToFirst()) {
int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(numberIndex);
// Do something with the phone number
...
}
}
}
I hope there isn't such a functionality.
The way you compare the "call without permissions" with the "read contacts without permissions" will be i think like the user has to enter the contactdata per hand.
The permissions have to be requested by app to protect the privacy of the user and helps to prevent data-collecting-apps.
If your app needs the contact list I think you can add the permission and the user will understand why you need it. If you don't need the contacts, you should not try the read the contacts.
An app always should only provide functionality that is really needed and nothing more.
If a desktop-programm will collect data of what you are doing on computer, what games you are playing, whit whom you are mailing, etc. you will call it a trojan.
So just read the contacts if its really needed and then the user will give you the permissions therefore.
The permission.system of android mostly make sense ;)
I have found, by experimentation, a few Contacts fields that can be queried successfully without READ_CONTACTS permission.
They are:
ContactsContract.Data.LOOKUP_KEY,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.DISPLAY_NAME_PRIMARY
However,
ContactsContract.CommonDataKinds.Phone.NUMBER
throws an IllegalArgumentException "invalid column: data1".
Similar exceptions were thrown by any of the other data columns that I tried.
I have tested this on many APIs, from 8 through 32. It fails in API 8, which requires READ_CONTACTS. It works in API 10 and up. I don't know about API 9.
In API 30+, any queries made must be announced in the AndroidManifest.xml <queries> elements.
This particular data makes sense in Android's strict security sense. This permits a client app to obtain a Uri by the user picking it from the Contacts app, together with an identifying name. The client app can later use this Uri to pull up the contact in the Contact app. No sensitive information is communicated -- the name is after all just a label that the user has applied to the contact.
I would love to know if and where this is documented.
I want is to hide all contacts without phone numbers in phonebook from my application..
Just like phonebook do, When you go to phonebook -> settings there is a checkbox which states that "Contacts with phone number only" I want to implement this feature in my app
I need a method (code) to navigate users to
phonebook -> settings (activity) (System app)
from my application activity.
or worse case hide all contacts without phone number through database. So that phonebook can filter out.
Currently i found
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 123);
Above code opens phone book but i want to open phone book -> settings page.
In sum i want to make phonebook contents "contacts with phone numbers" from my application
I need a method (code) to navigate users to phonebook -> settings (activity) (System app)
There are hundreds, perhaps thousands, of Android phones. None will necessarily be the same with respect to their "phonebook" app. None of those "phonebook" apps necessarily have the feature you seek -- some may, some many not. And, most likely, none have a documented and supported Intent structure for getting to a screen within the app to control the setting that they may or may not have.
I want is to hide all contacts without phone numbers in phonebook from my application
Then you will need to not use the "phonebook" app, but instead display contacts yourself, via the READ_CONTACTS permission and the ContactsContract ContentProvider.
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
Well, I have sucessfully developed a Contacts application for Android as my major project. I believe this is quite simple. Here is code how I did it.
Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
null,
ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1",
null,
ContactsContract.Contacts.DISPLAY_NAME+" COLLATE LOCALIZED ASC");
mAdapter = new MyAdapter(this,
R.layout.single_cell,
c,
new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},
new int[]{R.id.disp_name},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listview.setAdapter(mAdapter);
And, in MyAdapter, I have extended SimpleCursorAdapter and over rided bindView() to take advantage of efficiency of SimpleCursorAdapter. However, you need a permission to read contacts. In your android-manifest file. Please mention,
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Hope, it helps.
I have written code to add a number in contact book when the application is used.
ContentResolver cr = getContentResolver();
Uri phoneUri = null;
ContentValues values = new ContentValues();
values.put(People.NAME, "stack");
Uri uri = getContentResolver().insert(People.CONTENT_URI, values);
phoneUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
values.clear();
values.put(People.Phones.TYPE, People.Phones.TYPE_MOBILE);
values.put(People.Phones.NUMBER, "9879958170");
getContentResolver().insert(phoneUri, values);
and even added the permission in .manifest file.
<uses-permission android:name="android.permission.READ_CONTACTS"
android:enabled="true" />
<uses-permission android:name="android.permission.WRITE_CONTACTS"
android:enabled="true" />
this is working on emulator the contact gets added but..when i put this application into device....there is no contact with name stack...
Can anyone help me and let me know if i have done any mistake.
Thanks in advance:)
what phone do you have? Some phones (ex. samsung) have a modified contact-app installed, maybe thats the reason.
What version of android is your phone running? You're using the old contacts API here and if your phone is running Eclair or better, though these api calls should still technically work, they are not recommended. Especially when we're talking about a manufacturer customized phone app the results might be a little unpredictable.
Also, sometimes if you don't add your contact to the "My Contacts" system group, the contact will not appear in the contacts application. This is not true for every contacts app but I have seen it happen in a few cases.
Please give us a bit more info so we can answer your question properly. (Model of phone, android version its running, etc)
I've problem when I want to read com.android.email.provider to
get email accounts.
Here is my code to retrieve the account :
Cursor c = null;
Uri CONTENT_URI = Uri.parse("content://com.android.email.provider/account");
String RECORD_ID = "_id";
String[] ID_PROJECTION = new String[] {RECORD_ID };
c = getContentResolver().query(CONTENT_URI,ID_PROJECTION,null, null, null);
I got a security exception:
java.lang.SecurityException: Permission Denial: reading com.android.email.provider.EmailProvider uri content://com.android.email.provider/account from pid=278, uid=10003 requires com.android.email.permission.ACCESS_PROVIDER
I want to know the account is created or not in some other app. Is there any other way to resolve this so that I can read from the provider.
I have also tried adding the permission in manifest:
<uses-permission android:name="com.android.email.permission.ACCESS_PROVIDER"/>
That didn't help.
Any solution would be greatly appreciated.
Thanks in advance
I'm afraid, it's impossible. See, here http://androidbridge.blogspot.com/2011/03/reading-emails-in-android.html