繁体   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