Search code examples
androidandroid-intentbrowserandroid-manifestandroid-browser

How to get html code from browser when share menu is selected


I'm trying to make an app that will display the url and page title that I am currently viewing in the browser when I choose the share menu and select my app.

This is my Intent filter in the manifest.xml:

intent-filter

action android:name="android.intent.action.SEND"

category android:name="android.intent.category.DEFAULT" 

data android:mimeType="text/plain" 

intent-filter

This is the code in my activity:

public void onCreate(Bundle savedInstanceState) {    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView text = (TextView) this.findViewById(R.id.url_text);
    Intent intent = getIntent();
    if (savedInstanceState == null && intent != null) {
        Log.d(TAG, "intent != null");
        if (intent.getAction().equals(Intent.ACTION_SEND)) {
            Log.d(TAG, "intent.getAction().equals(Intent.ACTION_SEND)");
            url = intent.getStringExtra(Intent.EXTRA_TEXT);
            text.setText(url);          
        }
    }
}
}

This code is working to get the url of the page, but I can't figure out how to get the title of the page I'm viewing in the browser. One way of doing this which I got working was to use HttpResponse to execute a new request and then get the page content and search through that to get the title. Is there an easier way to find out the title of the page?

Any help would be much appreciated. Thanks.


Solution

  • I think the title comes to you in the EXTRA_SUBJECT Intent extra.