This question already has answers here:
What is the default Account Type / Name for contacts on Android Contact Application?
(3 answers)
Closed 5 years ago.
I would like to programmatically retrieve all accounts on a android phone/tablet that are able to be used for contacts, without any previous knowledge of the existents accounts.
I used AccountManager to retrieve all accounts, but this give me other accounts too (for example email accounts).
So I added AccountManager.hasFeatures() to select the only accounts that have the "service_contacts" feature, but in this way I only find the Google account and not others (for example on my phone I use Davdroid account for my contacts, and my program don't select this account).
So, where I'm wrong? Are there others API that I should use?
Can someone suggest me (maybe with some code as example) a way to the goal?
Thanks for any help
EDITED:
Sorry for my not perfect knowledge of english language, but I thought I explained well myself.
I don't want to retrieve an account that I already know, or by a type that I already know (getAccountsByType).
I want to deploy an Application on a phone that I don't know, that can have whatever account (maybe none Google account) with no any previous knowledge, and retrieve only that accounts able to be used for contacts (they are the accounts that you can select in the Contacts application when you add a new contact).
Maybe I'm wrong and I ask forgiviness for that, but I can't see this as a duplicated question, and I'm not able to understand how to use your answers to solve my very specific problem.
Thanks again for your answers
SOLUTION:
I used ContentResolver.getSyncAdapterTypes() to select all sync adapters related to "com.android.contacts", so the code is (I used an EditText for display):
private void getAccountsForContacts() {
// Constants
final String SYNC_AUTHORITY = "com.android.contacts";
// Variables
List<SyncAdapterType> okSyncs;
SyncAdapterType[] mySyncTypes;
AccountManager accountManager;
Account[] tempAccounts;
List<Account> allAccounts;
String textAccounts;
int i;
int j;
// Clear the text
EditText editListAccounts = (EditText) findViewById(R.id.editListAccounts);
editListAccounts.setText("");
// Initialize all variables
okSyncs = new ArrayList<SyncAdapterType>();
allAccounts = new ArrayList<Account>();
textAccounts = "";
// Get all sync adapters
mySyncTypes = ContentResolver.getSyncAdapterTypes();
if (mySyncTypes.length > 0) {
for (i = 0; i < mySyncTypes.length; i++) {
// Select the only adapters for contacts
if (mySyncTypes[i].authority.equals(SYNC_AUTHORITY)) {
okSyncs.add(mySyncTypes[i]);
}
}
}
if (okSyncs.size() > 0) {
accountManager = AccountManager.get(this);
// From sync get all related accounts
for (i = 0; i < okSyncs.size(); i++) {
tempAccounts = accountManager.getAccountsByType(okSyncs.get(i).accountType);
if (tempAccounts.length > 0) {
for (j = 0; j < tempAccounts.length; j++) {
allAccounts.add(tempAccounts[j]);
}
}
}
}
if (allAccounts.size() > 0) {
for (i=0; i<allAccounts.size(); i++) {
textAccounts += allAccounts.get(i).toString();
}
} else {
textAccounts = getResources().getString(R.string.main_accounts_not_found);
}
editListAccounts.setText(textAccounts);
}
I hope it can be useful for someone.
Thanks anyway.
I guess this one could help you solving it by yourself and learning things that way
AccountManager.getAccountsByType(java.lang.String)
Related
Good day all, I have a list of Objects (Let's call them ContactObject for simplicity). This object contains 2 Strings, Name and Email.
This list of objects will number somewhere around 2000 in size. The goal here is to filter that list as the user types letters and display it on the screen (IE in a recyclerview) if they match. Ideally, It would filter where the objects with a not-null name would be above an object with a null name.
As of right now, the steps I am taking are:
1) Create 2 lists to start and get the String the user is typing to compare to
List<ContactObject> nameContactList = new ArrayList<>();
List<ContactObject> emailContactList = new ArrayList<>();
String compareTo; //Passed in as an argument
2) Loop though the master list of ContactObjects via an enhanced for loop
3) Get the name and email Strings
String name = contactObject.getName();
String email = contactObject.getEmail();
4) If the name matches, add it to the list. Intentionally skip this loop if the name is not null and it gets added to the list to prevent doubling.
if(name != null){
if(name.toLowerCase().contains(compareTo)){
nameContactList.add(contactObject);
continue;
}
}
if(email != null){
if(email.toLowerCase().contains(compareTo)){
emailContactList.add(contactObject);
}
}
5) Outside of the for loop now as the object lists are build, use a comparator to sort the ones with names (I do not care about sorting the ones with emails at the moment)
Collections.sort(nameContactList, new Comparator<ContactObject>() {
public int compare(ContactObject v1, ContactObject v2) {
String fName1, fName2;
try {
fName1 = v1.getName();
fName2 = v2.getName();
return fName1.compareTo(fName2);
} catch (Exception e) {
return -1;
}
}
});
6) Loop through the built lists (one sorted) and then add them to the master list that will be used to set into the adapter for the recyclerview:
for(ContactObject contactObject: nameContactList){
masterList.add(contactObject);
}
for(ContactObject contactObject: emailContactList){
masterList.add(contactObject);
}
7) And then we are all done.
Herein lies the problem, this code works just fine, but it is quite slow. When I am filtering through the list of 2000 in size, it can take 1-3 seconds each time the user types a letter.
My goal here is to emulate apps that allow you to search the contact list of the phone, but seem to always to it quicker than I am able to replicate.
Does anyone have any recommendations as to how I can speed this process up at all?
Is there some hidden Android secret I don't know of that only allows you to query a small section of the contacts in quicker succession?
I'm building an Android app with the Realm database.
I have a RealmObject subclass called Article which has an id field (it's and int and also a #PrimaryKey). I would like to pass to a query a list of ints (a Set, int[], or whatever) of article id's and retrieve only those articles.
In SQL would be like this:
SELECT *
FROM `table`
where ID in (5263, 5625, 5628, 5621)
I've seen it's possible to do this in iOS in this StackOverflow question.
How can I do this in Android? Thanks!
Edit: Just to inform, I also asked this on the GitHub repo here.
Update:
Realm 1.2.0 has added RealmQuery.in() for a comparison against multiple values. The documentation details all the available overloads. This one is the method we can use if our ids are Integers:
public RealmQuery<E> in(String fieldName, Integer[] values)
Original answer:
The answer from #ChristianMelchior returns all articles if the list of ids is empty. I want it to return an empty RealmResults<Article>. That's what I've ended up doing:
Set<Integer> articleIds = this.getArticleIds();
RealmQuery<Article> query = realm.where(Article.class);
if (articleIds.size() == 0) {
// We want to return an empty list if the list of ids is empty.
// Just use alwaysFalse
query = query.alwaysFalse();
} else {
int i = 0;
for (int id : articleIds) {
// The or() operator requires left hand and right hand elements.
// If articleIds had only one element then it would crash with
// "Missing right-hand side of OR"
if (i++ > 0) {
query = query.or();
}
query = query.equalTo("id", id);
}
}
return query.findAll();
Now realm v 1.2.0 support RealmQuery.in() for a comparison against multiple values.
The Realm Java API's doesn't support this yet unfortunately. You can follow the feature request here https://github.com/realm/realm-java/issues/841
The current work-around would be to build up the query yourself in a for-loop:
RealmResults<Article> articles = realm.allObjects(Article.class);
RealmQuery q = articles.where();
for (int id : ids) {
q = q.equalTo("id", id);
}
RealmResults<Article> filteredArticles = q.findAll();
This is the way Realm does it since 1.2.0:
public RealmQuery<E> in(String fieldName, String[] values) {
if (values == null || values.length == 0) {
throw new IllegalArgumentException(EMPTY_VALUES);
}
beginGroup().equalTo(fieldName, values[0]);
for (int i = 1; i < values.length; i++) {
or().equalTo(fieldName, values[i]);
}
return endGroup();
}
Previously this is how I did it
I just came across this post and I thought I could throw in my 2 cents on this. As much as I appreciate Christian Melchior and his answers I think in this case his answer is not working (at least in the current version).
I prefer to do it like this - I personally think it's more readable than Albert Vila's answer:
List<String> listOfIds = [..];
RealmQuery<SomeClass> query = realm.where(SomeClass.class);
boolean first = true;
for (String id : listOfIds) {
if (!first) {
query.or();
} else {
first = false;
}
query.equalTo("id", id);
}
RealmResults<SomeClass> results = query.findAll();
this may be simple to you but I am stucked for hours. My Parse User have a relation I setup which is called "Friendsrelation". Now I wanted to load the list of user who are not a friend with the current user so that the currect user can send a request to add him as friend. I cant seems to be able to query users who are not friend (means not in friendsrelation) with currect user. Hope you can help me out here. Much appreciated.
Cheers!
Hi use this query :-
String qry = "select userid FROM users WHERE Not EXISTS (SELECT friendId FROM friendsrelation WHERE friendsrelation.friendId =users.userID and friendsrelation.myid = currentuserID)";
Cursor objCursor = db.rawQuery(qry,null);
ArrrayList<Long> friendsids = new ArrrayList<Long>();
if ((objCursor != null) && (objCursor.getCount() > 0)) {
objCursor.moveToFirst();
while (objCursor.isAfterLast() == false) {
friendsids.add(objCursor.getLong(objCursor.getColumnIndex("userid ")));
}
}
There is described on this page how android is able to select all user certificates and show them in a spinner...
http://source-android.frandroid.com/packages/apps/Settings/src/com/android/settings/vpn2/VpnDialog.java
They use the Method:
private void loadCertificates(Spinner spinner, String prefix, int firstId, String selected) {
Context context = getContext();
String first = (firstId == 0) ? "" : context.getString(firstId);
String[] certificates = mKeyStore.saw(prefix);
if (certificates == null || certificates.length == 0) {
certificates = new String[] {first};
} else {
String[] array = new String[certificates.length + 1];
array[0] = first;
System.arraycopy(certificates, 0, array, 1, certificates.length);
certificates = array;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
context, android.R.layout.simple_spinner_item, certificates);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
for (int i = 1; i < certificates.length; ++i) {
if (certificates[i].equals(selected)) {
spinner.setSelection(i);
break;
}
}
}
with this call:
loadCertificates(mIpsecUserCert, Credentials.USER_CERTIFICATE,
0, mProfile.ipsecUserCert);
now they use Credetials.USER_CERTIFICATE for the Method mKeyStore.saw(String prefix)
thats exactly what im looking for, but i cannot invoke the method saw and its also not mentioned in the android api...
so is there somehow a way of getting the same result for me?
the keystore library is not able of making a difference of user or system certs, and if i want to put all certs in a dropdown list it will cost me a lot of loading time. The other way of typing the subject name of the cert and looping through all certs till its the right one is not really practicable for a non-IT guy, so its crappy to handle for a normal user...
there are almost no references on this topic in the net, thats why im asking google android pro's on this post only.
thankyou
This is a private API, it cannot be used by third party applications. Why do you want to select a user certificate? You generally want the user's private key, and the KeyChain API supports letting the user select one of those.
I have to create an app in android with a database.In that database I have a predefined list of products.
Now,the thing is that my ap has to offer to the user the posibility to introduce in that list some other products which are not in the list.
To this end, I've created an autocomplete text view in which I introduce a new product and I take the text fro autocomplete and I have to write it in the database
Now,my problem is that when I display the products that I've introduced in the database,the toast text that I use to display what I have in the database it shows me nothing next to "product......".
Now,that may be because when I try to get the text from the autocomplete I get nothing in return?
This is how I read from autocomplete:
mItem = (AutoCompleteTextView) findViewById(R.id.todo_edit_item);
String nou=mItem.getText().toString();
And then I compare nou(which is what I wrote in the autocomplete) with what I have predefnied in the list,so if it is a new product(which was not in the list already) the I add it in the database:
for(int i = 0; i < l; i++)
{
if (nou!=fruits[i])
t=true;
else t=false;
}
if (t==true)
{
db.insertTitle(nou);
fruits=db.getAllfromDB("Fruits","fruit");
l=l+1;
}
Anyone any ideas of what I'm doing wrong in here cause I can't figure out.I'lll be here for further details.Thank u in advance:)
You compare strings using != instead of using !nou.equals(fruits[i]). also you compare to all elements in array each time, since you so t is always the value of the comparison to the last element in the array whether a match was found or not.
It should be written like that:
t = true;
for(int i = 0; i < l; i++)
{
if (nou.equals(fruits[i]))
{
t=false;
break;
}
}
if (t==true)
{
db.insertTitle(nou);
fruits=db.getAllfromDB("Fruits","fruit");
l=l+1;
}