簡體   English   中英

如何從意圖攔截的android webview中加載URL?

[英]How to load url in android webview intercepted from from intent?

我對android和java完全陌生,所以請問一些瑣碎的事情。 我有一個webview應用程序,只要有人從Google搜索或電子郵件中單擊我的網站網址,它都可以正常打開。 我在AndroidManifest.xml中放入了以下代碼[代碼1]。 這真的很好。 每當我在android上單擊我的網站的網址時,該應用就會打開。

問題是我無法從意圖中獲取url並將其加載到webview應用程序中。 例如,當用戶單擊鏈接www.mywebsite.com/contact時,它將打開應用程序並在webview元素中啟動默認的www.mywebsite.com。 發生這種情況是因為我尚未傳遞從Intent接收到的數據(URL)並將該URL加載到Webview中。

我有接收意圖並加載Webview [代碼2]的代碼,但是它不起作用。 每次我使用意圖打開應用程序時,應用程序崩潰(通過單擊Google搜索中的鏈接)。 我不知道將代碼放在MainActivity.java還是其他文件中。 這是一個小代碼,請讓我知道我在做什么錯。

[代碼1] ----------------------

<activity
   android:name=".activity.MainActivity"
   android:label="@string/app_name"
   android:launchMode="standard"
   android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
   <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>

   <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.mywebsite.com"
         android:pathPrefix="/"/>
   </intent-filter>

</activity>

[代碼1] ------------------------

[代碼2] ------------------------

if(getIntent().getData()!=null)
{
Uri data = getIntent().getData();
String scheme = data.getScheme();
String fullPath = data.getEncodedSchemeSpecificPart();

combine = scheme+"://"+fullPath;
}

String url = null;
if(combine!=null)
{
url = combine;
webview.loadUrl(url);
}

[代碼2] ------------------------

聲明合並為“字符串合並= null;” 在文件的開頭。

我從StackOverflow本身獲得了此[代碼2],但是我沒有得到將它放在哪個文件的哪個函數下的位置。

這是Play Consol崩潰部分的錯誤日志。

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2659)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2724)
  at android.app.ActivityThread.-wrap12 (ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1473)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:154)
  at android.app.ActivityThread.main (ActivityThread.java:6123)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:867)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:757)

Caused by: java.lang.NullPointerException: 
  at com.mytemplates.webviewapp.activity.MainActivity.onCreate (MainActivity.java:94)
  at android.app.Activity.performCreate (Activity.java:6672)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1140)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2612)

您應該在MainActivity的onCreate方法中放入(代碼2)部分,因為清單清單這一部分中定義的句柄方案的意圖過濾器,因此您應該在MainActivity類的onCreate方法中編寫(代碼2)。

     WebView webview = (WebView) findViewById(R.id.WebView);
     webview.getSettings().setJavaScriptEnabled(true);

     Intent intent=getIntent();
     String action = intent.getAction();

   if (Intent.ACTION_VIEW.equals(action) && (intent.getData() != null))
   {
   Uri data = intent.getData();
   String scheme = data.getScheme();
   String fullPath = data.getEncodedSchemeSpecificPart();
   combine = scheme+"://"+fullPath;
   }

String url = null;
if(combine!=null)
{
 url = combine;
 webview.loadUrl(url);
}

您可以使用此鏈接文檔獲取更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM