Make Android app listen to shared links

前端 未结 3 2046
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 08:21

I want my Android app to appear listed as an option when the user shares an URL from another app (like the browser). How can I register my app to do that? How can I react to

相关标签:
3条回答
  • 2020-12-01 09:05

    To get the image in your activity, use Uri imgUri = (Uri) i.getParcelableExtra(Intent.EXTRA_STREAM); for a single image, or use ArrayList<Uri> imgUris = i.getParcelableArrayListExtra(Intent.EXTRA_STREAM); for a list of images:

    Intent i = getIntent();
    Bundle extras = i.getExtras();
    String action = i.getAction();
    
    // if this is from the share menu
    if (Intent.ACTION_SEND.equals(action)) {   
        if (extras.containsKey(Intent.EXTRA_STREAM)) {
            Uri imgUri = (Uri) i.getParcelableExtra(Intent.EXTRA_STREAM);
            // Do job here
        }
    }
    

    Hope that helps

    0 讨论(0)
  • 2020-12-01 09:11

    You need to create an Activity with appropriate Intent filter. Read documentation about Intent, it explains all this with examples.

    0 讨论(0)
  • 2020-12-01 09:15

    At the very bare minimum you need:

    <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>
    </activity>
    

    in your manifest...which will at least make it show up in the 'share' listing.

    The most important part you are missing is:

    <action android:name="android.intent.action.SEND" />
    

    To make it actually do something, you need an Activity.

    This may help too: http://sudarmuthu.com/blog/sharing-content-in-android-using-action_send-intent

    Additional Info:

    <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" />
        <data android:mimeType="text/plain" />
    </intent-filter>
    </activity>
    

    There the <data android:mimeType will limit what you respond to, if you want to limit your app's response.

    0 讨论(0)
提交回复
热议问题