2017-03-27 117 views
0

我正在写一个浏览器,并希望实现一个按钮“添加网站到主屏幕”,以便网站图标(apple-touch-icon)和标题可以通过启动器获取并显示。Android将图标添加到主屏幕(启动器)

我发现这确实是代码 -

 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); 

但不幸的是,意图在的onCreate是没有任何额外的数据:

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

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

我应该如何通过额外parametr(URL)到发射器,以便进一步我可以读取它?

回答

0

试一下这个

intent.putExtra(EXTRA_URL, Uri.parse(webView.getUrl())); 
+0

不工作,仍然没有数据到达帐篷 –

0

所以我设法使用下面的代码来解决这个任务:

发送快捷方式启动:

 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正确的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); 
     } 
    }