2017-06-21 52 views
1

我有一個博客,我使用Webview製作了它的應用程序版本。問題是:我想通過其他應用程序分享我的帖子,例如發送WhatsApp消息或電子郵件給某人,並鏈接到位於我博客上的特定帖子(如www.blogspot.myblog.com/2017/postexample) ,然後可以選擇使用我的應用程序打開它,例如Facebook,甚至Youtube。 我做了這個:如何打開位於第三方應用程序或瀏覽器(如whatsapp消息)的url直接進入我的webview應用程序?

  <intent-filter> 
      <action android:name="android.intent.action.VIEW" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.BROWSABLE" /> 

      <data 
       android:host="mywebsite.com" 
       android:pathPrefix="/" 
       android:scheme="http" /> 
      <data 
       android:host="www.mywebsite.com" 
       android:pathPrefix="/" 
       android:scheme="http" /> 
     </intent-filter> 

它並啓動我的應用程序,但(當然)它打開應用程序的默認頁面,而不是www.blogspot.myblog.com/2017/postexample,所以我應該怎麼辦直接打開發送的網址?感謝球員,一個幫助將不勝感激。

編輯

這裏是我現在的代碼:

 <intent-filter > 
      <action android:name="android.intent.action.VIEW" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.BROWSABLE" /> 

      <data android:scheme="http" 
       android:host="www.blog.com" 
       android:pathPrefix="/" /> 
     </intent-filter> 
    </activity> 

以及活動

public class BlogActivity extends AppCompatActivity { 
private static final String TAG = "BlogActivity"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Intent intent = getIntent(); 
    String action = intent.getAction(); 
    Uri data = intent.getData(); 

    //Get the link from that Uri 
    if (data != null) { 
     Log.d(TAG, "Data" + data.toString()); 

     WebView WebView2 = (WebView)findViewById(R.id.web1); 
     WebView2.setWebViewClient(new WebViewClient()); 
     WebView2.loadData(TAG, "text/html; charset=utf-8", "UTF-8"); 
     WebSettings webSettings = WebView2.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 
    } 
} 

}

回答

1

看一看:https://developer.android.com/training/app-indexing/deep-linking.html

從上面的鏈接:

一旦系統通過一個意圖過濾器開始您的活動,您可以使用由意向提供的數據,以確定您需要渲染什麼。調用getData()和getAction()方法來檢索與傳入Intent相關的數據和操作。您可以在活動的生命週期中隨時調用這些方法,但通常應在早期回調(如onCreate()或onStart())中執行此操作。

這裏有一個片段,演示如何從一個Intent檢索數據:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Intent intent = getIntent(); 
    String action = intent.getAction(); 
    Uri data = intent.getData(); 

    //Get the link from that Uri 
    if(data != null) { 
     Log.d(TAG, "Data" + data.toString()); 
    } 
} 
+0

感謝您的回答,但我有點困惑。深層鏈接不會只將用戶發送到webview屏幕,因此是博客的默認頁面?我如何使用它將某人從第三方應用程序發送到webview中的特定URL?謝謝! – SkelS

+0

查看更新,你應該可以從數據中獲得鏈接Uri – Isaac

+0

謝謝,但我無法完成工作,我對此很感興趣。我做了一個新的活動,儘管主要的一個,我在那裏放置了一個新的WebView的腳本,但我不知道如何使用腳本得到的字符串的URL。這是錯誤的: WebView2.loadData(action,「text/html; charset = utf-8」,「UTF-8」); 因爲它確實打開了應用程序,所以Intent正在工作,但我仍然無法從中收集信息或不知道如何使用它。謝謝 – SkelS

相關問題