Predefined actions of application. Link to my application from Contacts

二次信任 提交于 2019-12-21 18:40:24

问题


I want to write application which is connected with Contacts.

Scenario :

  1. Enter to phone Contacts
  2. We choose Contact item

  3. And icon of my application should appear in QuickAction Dialog.

  4. I click on my app icon and Aplication start with data from contact record.

What I have to add to AndroidManifest to do it?


回答1:


Add this intent filter for your app to be visible for all contacts.

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="vnd.android.cursor.item/name" />
    </intent-filter>

Change the mimetype, so that only contacts with a particular data have your activity.

e.g if you want your activity to show only for contacts with email then change mimetype to vnd.android.cursor.item/email_v2 . You can get the mimetype names from the subclasses of DataColumns




回答2:


Exactly this behavior is possible only when your app in an only app on users device which is capable to process desired type of content. For example, if you want to use type of content "send SMS", or "make a phone call" - your app most likely is not the only app on the device which can process such actions (there are also stock phone dialer and SMS apps).

Anyway, you can always add your application to the list of apps which will appear when user clicks to that quick actions. It will look like this:

In order to do this, you need:

  • specify in your manifest file that your application is capable to deal with desired action, for example "sending SMS". To do this you need to add intent filter to your Activity, that you wish to be called in that case:

    <intent-filter>
        <action android:name="android.intent.action.SENDTO" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="sms" />
        <data android:scheme="smsto" />
    </intent-filter>
    
  • in your target Activity add code, that process desired content type. In your target Activity onCreate() method, or onNewIntent() method query data string parameter from calling Intent. Here is some sample code to demonstrate general idea:

    public class MyActivity extends Avtivity {
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String dataStr = intent.getDataString();
        // do some processing with dataStr 
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent callIntent = getIntent();
        String dataStr = callIntent.getDataString();
        // do some processing with dataStr 
    }
    }
    


来源:https://stackoverflow.com/questions/12383692/predefined-actions-of-application-link-to-my-application-from-contacts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!