Search code examples
javaandroidandroid-intentandroid-activity

How can I open my Android app through share screen?


I want my app to be shown on the share screen dialogue of android. Like when we using our file manager and want to open any file (let's say) with mp4 extension. Then we'll get the list of all the mp4 media player of our phone. Or if you choose to share any file then we'll get the list of all the apps that can share the file like WhatsApp, xender, etc. Similarly, I wanted to open .txt file In my app Text To UI (app name). Though I already know how to open the file manually.


Solution

  • Add below code in Manifest under that activity which is open on share

    If MainActivity is not launcher activity, Add below code in Manifest

    <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.SEND" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <data android:mimeType="*/*"/>
                </intent-filter>
    </activity>
    

    If MainActivity is your launcher activity and as well as that activity which is open on share, Add below code in Manifest

    <activity
                android:name=".MainActivity"
                android:screenOrientation="portrait">
                 <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
                <intent-filter>
                    <action android:name="android.intent.action.SEND" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <data android:mimeType="*/*"/>
                </intent-filter>
            </activity>
    

    MimeType "text/*" is for send only text type data.

    MimeType "image/*" is for send only images type data.

    MimeType "video/*" is for send only video type data.

    MimeType "*/*" is for capable to send all type of data like video,audio,image,text

    For getting data in your App try below code in activity:

     Intent receiverdIntent = getIntent();
            String receivedAction = receiverdIntent.getAction();
            String receivedType = receiverdIntent.getType();
    
            if (receivedAction.equals(Intent.ACTION_SEND)) {
    
    
                Uri receiveUri = (Uri) receiverdIntent
                        .getParcelableExtra(Intent.EXTRA_STREAM);
    
                if (receiveUri != null) {
                    Log.e("Shared",receiveUri.toString());
                }else{
                    Log.e("Shared","Nothing");
                }
    
            }
    

    App is in sharing list:

    enter image description here

    I hope it works for you.