简体   繁体   中英

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. I have a webview app which opens fine whenever someone clicks my website url from google search or from email. I have put following code [Code 1] in AndroidManifest.xml . This is working really well. Whenever i click url of my website on android, the app opens.

Problem is I am not able to get the url from the intent and load that in the webview app. 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. This is happening because i have not yet passed the data (url) received from intent and load that url in the webview.

I have the code to receive the intent and load webview [Code 2] but it is not working. The app crashed every time I open the app using intent (by clicking link from google search). I don't know where to put that code whether in MainActivity.java or in some other file. It's a small code, please let me know what i am doing wrong.

[Code 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] ------------------------

[Code 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] ------------------------

Declared combine as 'String combine = 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.

This is the error log from Play Consol crash section.

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.

     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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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