简体   繁体   English

如何从意图拦截的android webview中加载URL?

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

I am completely new to android and java so pardon if asking something trivial. 我对android和java完全陌生,所以请问一些琐碎的事情。 I have a webview app which opens fine whenever someone clicks my website url from google search or from email. 我有一个webview应用程序,只要有人从Google搜索或电子邮件中单击我的网站网址,它都可以正常打开。 I have put following code [Code 1] in AndroidManifest.xml . 我在AndroidManifest.xml中放入了以下代码[代码1]。 This is working really well. 这真的很好。 Whenever i click url of my website on android, the app opens. 每当我在android上单击我的网站的网址时,该应用就会打开。

Problem is I am not able to get the url from the intent and load that in the webview app. 问题是我无法从意图中获取url并将其加载到webview应用程序中。 For example when a user click on a link www.mywebsite.com/contact, it opens the app and launches the default www.mywebsite.com in webview element. 例如,当用户单击链接www.mywebsite.com/contact时,它将打开应用程序并在webview元素中启动默认的www.mywebsite.com。 This is happening because i have not yet passed the data (url) received from intent and load that url in the webview. 发生这种情况是因为我尚未传递从Intent接收到的数据(URL)并将该URL加载到Webview中。

I have the code to receive the intent and load webview [Code 2] but it is not working. 我有接收意图并加载Webview [代码2]的代码,但是它不起作用。 The app crashed every time I open the app using intent (by clicking link from google search). 每次我使用意图打开应用程序时,应用程序崩溃(通过单击Google搜索中的链接)。 I don't know where to put that code whether in MainActivity.java or in some other file. 我不知道将代码放在MainActivity.java还是其他文件中。 It's a small code, please let me know what i am doing wrong. 这是一个小代码,请让我知道我在做什么错。

[Code 1] ---------------------- [代码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>

[Code 1] ------------------------ [代码1] ------------------------

[Code 2] ------------------------ [代码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);
}

[Code 2] ------------------------ [代码2] ------------------------

Declared combine as 'String combine = null;' 声明合并为“字符串合并= null;” at the start of the file. 在文件的开头。

I got this [Code 2] from StackOverflow itself but i am not getting where should i put this, in which file, under what function. 我从StackOverflow本身获得了此[代码2],但是我没有得到将它放在哪个文件的哪个函数下的位置。

This is the error log from Play Consol crash section. 这是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)

you should put (Code 2) section in onCreate method in your MainActivity, because your intent-filter for handle scheme defined in this section of your manifest ,so you should write (code 2) in onCreate method of your MainActivity class. 您应该在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);
}

you can use this link document for more info 您可以使用此链接文档获取更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM