How to make my Android app appear in the share list of another specific app

假装没事ソ 提交于 2019-11-26 15:19:51
DanO

In order to do this, you need to know the Intent that the application is creating and create an IntentFilter that will add your application to the specific list.

Receiving an Implicit Intent on Intents and Filters (Android Developers)

The application probably uses a specific action name that you could hook to.

<intent-filter . . . >
    <action android:name="com.example.project.SHOW_CURRENT" />
    <action android:name="com.example.project.SHOW_RECENT" />
    <action android:name="com.example.project.SHOW_PENDING" />
    . . .
</intent-filter>

Or it could be looking for applications accepting a certain type of file.

<intent-filter . . . >
    <data android:mimeType="video/mpeg" android:scheme="http" . . . /> 
    <data android:mimeType="audio/mpeg" android:scheme="http" . . . />
    . . .
</intent-filter>

The name of the application and what it is sharing would help me give a more specific response.

Add this code in the activity you want opened first when sharing a content from outside the app, call this method in onCreate()

private void onSharedIntent() {
    Intent receiverdIntent = getIntent();
    String receivedAction = receiverdIntent.getAction();
    String receivedType = receiverdIntent.getType();

    if (receivedAction.equals(Intent.ACTION_SEND)) {

        // check mime type 
        if (receivedType.startsWith("text/")) {

            String receivedText = receiverdIntent
                    .getStringExtra(Intent.EXTRA_TEXT);
            if (receivedText != null) {
                //do your stuff
            }
        }

        else if (receivedType.startsWith("image/")) {

            Uri receiveUri = (Uri) receiverdIntent
                    .getParcelableExtra(Intent.EXTRA_STREAM);

            if (receiveUri != null) {
                //do your stuff
                fileUri = receiveUri;// save to your own Uri object

                Log.e(TAG,receiveUri.toString());
            }
        }

    } else if (receivedAction.equals(Intent.ACTION_MAIN)) {

        Log.e(TAG, "onSharedIntent: nothing shared" );
    }
}

Add this in Manifest,

 <activity
            android:name="your-package-name.YourActivity">
            <intent-filter>
                <action android:name="android.intent.action.SEND" /> 

                <category android:name="android.intent.category.DEFAULT" />      
                <data android:mimeType="image/*" />
                <data android:mimeType="text/*" />
            </intent-filter>
        </activity>

this worked well for me for getting all web pages, for my app that scans for mp3 files on a web page, and sets alarms from them. It opens up my new url activity, when you share a web page:

Here is what this code results in:

  <activity
        android:name=".NewUrl"
        android:label="@string/title_activity_new_url"
        android:windowSoftInputMode="stateUnchanged">
        <intent-filter>
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/*"/>
        </intent-filter>
    </activity>

Then to receive the link in the app, I got awsome info from this tutorial: http://code.tutsplus.com/tutorials/android-sdk-receiving-data-from-the-send-intent--mobile-14878

add this to your mainefist file

<activity android:name=".ShareActivity">
<intent-filter
    android:label="Share with my app">
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

this link may help you

- Add below code into your Project AndroidManifest.xml file in Specific Activity.

     <activity
                android:name=".MainActivity"
                android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
     </activity>

- Add the following line of code into your project specific Activity.

    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();
    if ("android.intent.action.SEND".equals(action) && type != null && "text/plain".equals(type)) {
                Log.println(Log.ASSERT,"shareablTextExtra",intent.getStringExtra("android.intent.extra.TEXT"));
            }

I would check to see if there is any API for this app you want to work with.

If so, you can benefit by knowing

  • a more specific implicit action for your filter
  • or perhaps add a category other than DEFAULT
  • If you can find something like these, it would be unlikely to be seen by other apps.

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