简体   繁体   中英

load url from browsable intent android

I implemented "< category android:name="android.intent.category.BROWSABLE" />" in my browser app and i want any other browser showing the list to choose which app to handle that link in, its working fine i am able to launch my app from the list but however it doesnt load that url in my app, all it does is open my app and paste the url in the text field. any clue?

edit: i tried this one for loading url in my webview after pasting it but the app force closes

TextView uri = (TextView) findViewById(R.id.urlField);
    //if (Intent.ACTION_MAIN.equals(getIntent().getAction())) {
        String intentUri = (new Intent("com.example.browsableintent.MY_ACTION"))
                .toUri(Intent.URI_INTENT_SCHEME).toString();
        uri.setText(intentUri);
        webView.loadUrl(intentUri);
        Log.w("URLHandler", intentUri);
    //} else {
        Uri data = getIntent().getData();
        if (data == null) {
            uri.setText("");
        } else {
            uri.setText(getIntent().getData().toString());
        }
    //}

manifest

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.APP_BROWSER" />
        </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" />
            <data android:scheme="https" />
            <data android:scheme="about" />
            <data android:scheme="javascript" />
        </intent-filter>

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

            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />

            <data android:scheme="http" />
            <data android:scheme="https" />
            <data android:scheme="inline" />
            <data android:mimeType="text/html" />
            <data android:mimeType="text/plain" />
            <data android:mimeType="application/xhtml+xml" />
            <data android:mimeType="application/vnd.wap.xhtml+xml" />
        </intent-filter>
        <!-- For viewing saved web archives. -->
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />

            <data android:scheme="http" />
            <data android:scheme="https" />
            <data android:scheme="file" />
            <data android:mimeType="application/x-webarchive-xml" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.WEB_SEARCH" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data android:scheme="" />
            <data android:scheme="http" />
            <data android:scheme="https" />
        </intent-filter>

LOG

02-11 17:50:48.678: E/AndroidRuntime(29023): FATAL EXCEPTION: main
02-11 17:50:48.678: E/AndroidRuntime(29023): Process: com.air.swiftmp, PID: 29023
02-11 17:50:48.678: E/AndroidRuntime(29023): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.air.swiftmp/com.air.swiftmp.MainActivity}: java.lang.NullPointerException
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.app.ActivityThread.access$800(ActivityThread.java:163)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.os.Handler.dispatchMessage(Handler.java:102)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.os.Looper.loop(Looper.java:157)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.app.ActivityThread.main(ActivityThread.java:5335)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at   java.lang.reflect.Method.invokeNative(Native Method)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at java.lang.reflect.Method.invoke(Method.java:515)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at dalvik.system.NativeStart.main(Native Method)
02-11 17:50:48.678: E/AndroidRuntime(29023): Caused by: java.lang.NullPointerException
02-11 17:50:48.678: E/AndroidRuntime(29023):    at com.air.swiftmp.MainActivity.onCreate(MainActivity.java:95)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.app.Activity.performCreate(Activity.java:5389)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-11 17:50:48.678: E/AndroidRuntime(29023):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)

I'll get straight to the point, if

all it does is open my app and paste the url in the text field.

that would mean that

if (Intent.ACTION_MAIN.equals(getIntent().getAction())) {

returns true

I'll assume that your browsable intent is also the main intent of the application.

So what your 'if' is doing is checking if your 'Intent' (that is also your browsable) is the 'main intent', which would be true.

EDIT:

Alright, basically you're checking if your browsable screen that is being opened is the screen your app should show when it's being openend.

In your android Manifest you'll have something like

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

which is your browsable intent. when you call getIntent().getAction() it will return the same value as Intent.MAIN_ACTION. That is because you defined it in action in your manifest. Nothing wrong with this.

<action android:name="android.intent.action.MAIN" />

Secondly you want to load the URL that your app gets when your screen opens. I do not have any experience with this, but I highly recommend to look at this link, since it seems to explain about loading URLs in your app with webViews.

I hope this provides you with enough answers and resolved some confusion!

EDIT2:

I'm going to guess that you havnt, either initialized your webView, or havnt set the contentView on webView

so try to replace your

setContentView(<Arbitary Thing refering to most likely a layout>);

to

 WebView webview = new WebView(this);
 setContentView(webview);

Since you'll be refering to another layout, you cannot load the xml for your TextView, so I'd advise to remove

TextView uri = (TextView) findViewById(R.id.urlField);
 uri.setText(intentUri);

and anywhere else you might refer to uri.

I would also recommend reading the examples provided in this link, since it looks like a good place to start. Other then that, just try and google for WebView tutorials.

And looking at your Log, at line 95. Of your MainActivity something returns null. If what I suggested doesn't work, can you show me what is on line 95?

EDIT3:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       WebView webview = new WebView(this);
       setContentView(webview);

       webView.loadUrl("http://www.google.com");

    }
}

And add this to your android manifest just below the tag

<uses-permission android:name="android.permission.INTERNET" />

/**********************************************************************/

Since you're new to android let me explain what most likely was causing your error:

When you do

setContentView(R.id.layout_name);

Any references made to R.id will look in layout_name.xml which is set by your setContentView. So if you have layout_name_2.xml with let's say R.id.urlField_2 and you try to do

TextView uri = (TextView) findViewById(R.id.urlField2);

it will not find R.id.urlField2, since it isnt defined in your layout_name. The activity will only look in the layout specified in setContentView() . There are ways to combine layouts, so you can use multiple xml's inside 1 activity, but that's a topic for another day.

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