0

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)
User_420
  • 83
  • 2
  • 11

1 Answers1

1

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.

Bart de Ruijter
  • 917
  • 10
  • 21
  • Try editing your question and show your current code and the error log of your crash. It's hard to help you if I don't know what I'm looking for! – Bart de Ruijter Feb 11 '15 at 12:03
  • line 95 is "Log.w("URLHandler", intentUri);" and i have experience with webviews, my webview is just fine – User_420 Feb 11 '15 at 12:54
  • what i want is whenever user looks up a video on google , as you know he/she would probably see youtube videos in search results so when they tap that link, it should ask them what do they want that link to open in ( **which is working fine** ) and i am also able to launch my app and get it paste the URL in the address bar of my app. **But** i want it to load that URL right after it pastes it there. – User_420 Feb 11 '15 at 13:12
  • Then I sadly have to say I wouldn't know what could be wrong now, I'd have to look up on it later, since right now I am busy. Sorry for the inconvience! – Bart de Ruijter Feb 11 '15 at 13:12
  • Okay, I'm sorry about that. But basically to put everything together: You want that a user will click a link to a video (for example from youtube), when they select your app to open it, you want to load the URL on the screen to play the video for the user? If so, try to take a look at [this](http://stackoverflow.com/questions/11411792/android-sdk-media-player-video-from-url) link – Bart de Ruijter Feb 11 '15 at 18:20
  • i'm playing the video in frame layout, it would be better if the URL loads within the webview and gets frame layout handle it – JRE.exe Feb 12 '15 at 11:10
  • btw i got the **manifest** part of my code from https://github.com/anthonycr/Lightning-Browser/blob/master/AndroidManifest.xml and the **java** part i got from https://github.com/commonsguy/cw-advandroid/tree/master/Introspection/URLHandler (original question- http://stackoverflow.com/questions/6049290/android-browsable-intent-api-level/6050687?noredirect=1#comment45233174_6050687 ) so the java part must be there in the Lightening Browser project on GitHub – JRE.exe Feb 12 '15 at 11:14
  • i tried looking Lightening Browser's classes but no clue :/ can you give it a try i think it must be there – JRE.exe Feb 12 '15 at 11:15
  • i think if i get the URL from text field and load it in webview settings, it can do the trick, something like "webView.loadUrl(urlEditText.getText());" but i cant get it working the error it give me is "The method loadUrl(String) in the type WebView is not applicable for the arguments (Editable)" – JRE.exe Feb 13 '15 at 07:44
  • Try urlEditText.getText().toString() – Bart de Ruijter Feb 13 '15 at 08:21