BroadcastReceiver for contact viewing - android

I'm trying to design a broadcast receiver for contacts, means it opens my app when other apps or the user wants any contact details. is there any means to do it(i didnt find it in intent filter). or i have to design my own?
basically when any app is trying to access a contact, that request goes through my app. it is just like BroadcastReceiver for change detection in contacts, I want it for when app wants to access a contact.

What you should do:
BroadcastReceivers are not designed for getting info back, instead you can implement a pattern just like the Contacts APIs do it, by implementing your own ContentProvider.
Create a ContentProvider in your app, to allow other apps to query on your ContentProvider just like they do on the Contacts ContentProvider.
See tutorial.
You need to design how you want your content uris to look like, for example:
vnd.android.cursor.item/my_app/contact to get info about a single contact
And you should also consider adding custom permission to your <provider> in your manifest, so also permitted apps can access your data.
Original Answer:
If you already have an Activity for viewing a contact, in your AndroidManifest, add the following intents:
<activity
android:name="..."
...>
<intent-filter>
<action android:name="com.android.contacts.action.QUICK_CONTACT" />
<action android:name="android.provider.action.QUICK_CONTACT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/contact" />
<data android:mimeType="vnd.android.cursor.item/person" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/person" />
<data android:mimeType="vnd.android.cursor.item/contact" />
<data android:mimeType="vnd.android.cursor.item/raw_contact" />
</intent-filter>
</activity>
You'll need to properly handle incoming intents with those mimetypes and data uris in your Activity.
You can an example in the Android system Contacts app manifest: https://android.googlesource.com/platform/packages/apps/Contacts/+/master/AndroidManifest.xml#307
You can test your implementation by adding a widget called "Contact 1x1" (the name may vary depending on the device) to your homescreen, and selecting which contact the widget should launch.
When clicking this widget it should call one of the above intents.

Related

Android: Accept data from other apps

I am working on article reader and am trying to accept article urls from other apps (similar to how Pocket, Evernote are listed in the Share via menu as "Add to X").
My activity contains the following in the manifest:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAUlT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="text/*" />
</intent-filter>
But so far the app only shows up in the "Share via" list for Chrome(Android), but not for other apps that show articles (for example, it doesn't show up in the share list in the "Reddit Now" app whereas other apps like "Pocket" or "Evernote" do).
What do I have to specify to accept any kind of information that may potentially contain an article url?

How can I have my app appear in the intent chooser only for certain urls?

I am developing an app that can extract information from certain web pages. The idea is that when the user is within a specific url path in the browser and press the share button, my app will show up in the list of receiver apps.
I can do that easily by adding this to the manifest:
<intent-filter android:label="#string/app_name" >
<action android:name="android.intent.action.SEND" />
<data android:mimeType="text/plain" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
However, this will make my app appear on the list on all urls, also all those where it will have nothing to do. Instead I would like the app appear in the chooser only from these urls:
www.example.com/foo/bla.html
www.example.com/foo/bar/blabla.html
But not from these:
www.example.com
www.foobar.com
etc. Ie. only from within a certain path on a certain host. Also note that I do not want my app to be launched when the user clicks on links matching the criteria. It should only be invoked from the share menu.
So my question is: How can I limit my app to show up in the intent choose only for certain urls?
Add the following filter to the destination Activity ..
1. host your site name.
2. scheme scheme of your site http or https.
3. path for the file path your app should display.
<intent-filter>
<data
android:host="www.example.com"
android:scheme="http"
android:path="/foo/bla.html"
/>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" >
</category>
<category android:name="android.intent.category.BROWSABLE" >
</category>
</intent-filter>
How can I limit my app to show up in the intent choose only for certain urls?
You can't. ACTION_SEND has no notion of "only from certain URLs", or "only from certain WhatsApp users", or "only from left-handed Alaskan pipe-welders".
This is not significantly different than any other activity. An <intent-filter> only controls what the Intent is being delivered to. Nothing controls what the Intent is delivered from, with the exception of security-related items (permissions, whether or not the component is exported, etc.).
So, with ACTION_VIEW, the URL is something being delivered to another activity ("I wish to view this URL"). This works because the URL is turned into the Uri of the Intent, and that can be filtered upon via <data> elements in the <intent-filter>.
But ACTION_SEND doesn't have to have a URL, and even if there is one, it is merely payload in the form of an extra. It is not something that can be filtered upon.
Hence, what you want is not supported.
Use pathPrefix
<intent-filter>
<data
android:host="www.example.com"
android:scheme="http"
android:pathPrefix="/foo/"
/>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" >
</category>
<category android:name="android.intent.category.BROWSABLE" >
</category>
</intent-filter>

intent-filter for a service : using a custom mime type

I want to add a custom mime so that my Android Service or a BroadcastReceiver would detect my custom files.
I have seen answers to questions like How to add custom mime type? and How to add custom mime type?
The answers here are shown for an Activity. But what modifications are required for service or BroadcastReceiver? I tried the same code for them, but it did not work.
I am new to android development. Some nice and detailed explanation is welcome. Is it possible?
Where am I going wrong.
The code that I used is :
<service android:name="XcardReceivedService"
android:exported="true"
android:enabled="true">
<intent-filter>
<!-- <action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
-->
<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.Xcard" />
<data android:host="*" />
</intent-filter>
</service>
but before the activity is started I want to add the data from that file to the database
You have to do that in the activity.
or that purpose I wanted to write a service or a broadcast receiver.
Files do not open in to a service or a broadcast receiver.
Is there any other possible way?
Put your code into your activity.
how should i differentiate between "how the activity was started" whether from other activity or when user clicked on this file.
Examine the Intent that was used to start your activity. You get this Intent via getIntent(). You can call getData() on the Intent to get the Uri associated with it. If this is not null, it should be the path to your file.

Intent filter data attribute parameters for dialer integration

I created a dialer for android. I integrated it with default dialer. Whenever I click call I get complete action using dialog. My App is fine till this point.
I want my app as one of the complete action using options only when the number being dialed is 10+ digits
Now what I want is to avoid some numbers like USSD codes, premium number etc, which can be in user's contact book. So Whenever user clicks on call USSD codes or premium numbers from dialer, I don't want my app as one of the options. How to set intent filter for this. I am assuming my filter has to be in the manifest.
My app should show up only when the number being dialed is 10+ digits
I have found examples for mime type, URLs, but could not find any example for dialer.
here is my manifest entry for activity
<activity android:name=.ActivityCallMore"
android:theme="#android:style/Theme.NoTitleBar" android:screenOrientation="portrait"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.CALL_PRIVILEGED" />
<action android:name="android.intent.action.DIAL" />
<action android:name="android.intent.action.CALL_BUTTON" />
<!-- <action android:name="android.intent.action.VIEW" /> -->
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="tel" />
</intent-filter>
</activity>
Thanks in advance.
Sorry, I don't think this can be done.
I imagined that it would be something like
<data android:pathPattern="...........*" />
but http://developer.android.com/guide/topics/manifest/data-element.html points out that pathPattern is
meaningful only if the scheme and host attributes are also specified for the filter.
and there is no host to specify in a tel://0123456789 URI. A similar problem is listed as a "medium priority defect" since 2009 at https://code.google.com/p/android/issues/detail?id=3368.
Let's hope someone else can prove me wrong.

How to integrate your app in QUICK CONTACT on the native contact app on android?

How to integrate your app in QUICK CONTACT on the native contact app? I want to see my apps logo. User should choose it for texting.I want the changes to be in manifest file and not through JAVA code.
I think I know now what you mean. I don't think it is possible through the manifest file. You need to add a profile action.
Please check the SampleSyncAdapter for the way to do it.
You can even add more actions and when the user clicks on the icon, the list of the available actions is shown.
Then you need to handle the action but you said you don't care about that...
Check also this:
Profile action
So you need to create your own sync adapter and create your raw-contacts. Only Contacts that have a Raw_Contact with your profile will show the icon. The shown icon is that one defined in your authenticator.xml file.
Right that is what I used too:
<intent-filter>
<action
android:name="android.intent.action.SENDTO" />
<data
android:scheme="sms" />
<data
android:scheme="smsto" />
<category
android:name="android.intent.category.DEFAULT" />
</intent-filter>
Then in the onCreate and in the onNewIntent you need to get the intent (use getIntent() in onCreate). From the intent check the action by using intent.getAction();
If (action.equalsIgnoreCase(Intent.ACTION_SENDTO)) you need to handle the sending of your message. With intent.getData() you get the uri of the contact.
//Not quite there yet but close.
<intent-filter>
<action
android:name="android.intent.action.SENDTO" />
<data
android:scheme="sms" />
<data
android:scheme="smsto" />
<category
android:name="android.intent.category.DEFAULT" />
</intent-filter>

Categories

Resources