0

I am writing a browser, and want implement a button "add site to home screen" so the site icon (apple-touch-icon) and title would be fetched by launcher and displayed.

I have found the code which does that -

        Intent shortcutIntent = new Intent();
        shortcutIntent.setClassName(getPackageName(), "Browser");
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        shortcutIntent.addCategory(Intent.ACTION_PICK_ACTIVITY);
        shortcutIntent.putExtra("EXTRA_URL", webView.getUrl());

        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, webView.getTitle());
        intent.putExtra(Intent.EXTRA_ORIGINATING_URI, webView.getUrl());

        //TODO change to apple-touch icon
        Bitmap btm = webView.getFaviconImage();
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, btm);

        intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        sendBroadcast(intent);

But, unfortunately, intent in onCreate is without any extra data :

    if (getIntent() != null){
       Intent intent = getIntent();

        //empty
        String url1 = intent.getStringExtra("EXTRA_URL");
    }

How should I pass extra parametr (URL), to launcher so that further i could read it ?

Anton Kizema
  • 1,072
  • 3
  • 13
  • 27

2 Answers2

0

Try this

intent.putExtra(EXTRA_URL, Uri.parse(webView.getUrl()));
AlexGuerra
  • 179
  • 7
0

So I managed to solve this task using next code :

Send shortcut to launcher:

        Intent shortcutIntent = new Intent(getApplicationContext(), BrowserActivity.class);
        shortcutIntent.putExtra(BrowserUnit.EXTRA_URL, webView.getUrl());

        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, webView.getTitle());

        //TODO change to apple-touch icon
        Bitmap btm = webView.getFaviconImage();
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, btm);

        intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        sendBroadcast(intent);

Retreive proper url:

       //in onCreate
       if (getIntent() != null){
        //if we are launching from launcher shortcut
        Bundle bundel = getIntent().getExtras();
        if (bundel != null) {
            String url = bundel.getString(BrowserUnit.EXTRA_URL);
            onSearchPressed(url);
        }
    }
Anton Kizema
  • 1,072
  • 3
  • 13
  • 27