You know when an unknown number calls you and instead of the number you see the name of the company which is actually calling you. As far as I know Android gets this information through some google services.
What should I do if I want to make an app, that does the same thing? If an unknown number calls you, I want Android to ask my app to provide info for this number.
My search found this in the documentation. Here is said:
The most important use case for Directories is search. A Directory provider is expected to support at least Contacts.CONTENT_FILTER_URI. If a Directory provider wants to participate in email and phone lookup functionalities, it should also implement CommonDataKinds.Email.CONTENT_FILTER_URI and CommonDataKinds.Phone.CONTENT_FILTER_URI.
As far as I understand, I need to implement a ContactsContract.Directory. I tried something but I can't get it to work.
Can you tell me what should I do, can you refer me to a nice explanation of how contact info providing works? A nice example for this Directory would be nice too.
EDIT: What I tried - I created a sample app, created my own provider and registered it in the manifest. I added the required meta data, as shown in the documentation:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.contactprovidertest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<provider
android:authorities="com.sample.contactprovidertest"
android:name=".MyProvider"
android:enabled="true"
android:exported="true">
<meta-data
android:name="android.content.ContactDirectory"
android:value="true"/>
</provider>
</application>
</manifest>
MyProvider.java
public class MyProvider extends ContentProvider {
private static final String TAG = "MyProvider";
@Override
public boolean onCreate() {
return true;
}
@Nullable
@Override
public Cursor query(Uri uri,
String[] projection,
String selection,
String[] selectionArgs,
String sortOrder) {
// this is never called
Toast.makeText(getContext(), "it works!", Toast.LENGTH_SHORT).show();
if (ContactsContract.Directory.CONTENT_URI.equals(uri)) {
Log.d(TAG, "query: " + uri);
}
return null;
}
@Nullable
@Override
public String getType(Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
}