从浏览的意图机器人负载网址 [英] load url from browsable intent android

查看:127
本文介绍了从浏览的意图机器人负载网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了<类机器人:名字=android.intent.category.BROWSABLE/>
在我的浏览器的应用程序,我想展示给选择处理,其做工精细的环节,我能够从列表中启动我的应用程序,它的应用程序,但但它不加载该网址在我的应用程序列表中的任何其他的浏览器,它所做的一切是打开我的应用程序,并粘贴在文本字段中的URL。任何线索?

编辑:我试图粘贴后,但应用程序强制关闭

在我的WebView这一个装载网址

  TextView的URI =(的TextView)findViewById(R.id.urlField);
    //如果(Intent.ACTION_MAIN.equals(getIntent()的getAction())){
        字符串intentUri =(新意图(com.example.browsableintent.MY_ACTION))
                .toUri(Intent.URI_INTENT_SCHEME)的ToString();
        uri.setText(intentUri);
        webView.loadUrl(intentUri);
        Log.w(URLHandler,intentUri);
    //}其他{
        乌里数据= getIntent()的getData()。
        如果(数据== NULL){
            uri.setText();
        }其他{
            uri.setText(。getIntent()的getData()的toString());
        }
    //}

清单


                

 <类机器人:名字=android.intent.category.DEFAULT/>
            <类机器人:名字=android.intent.category.LAUNCHER/>
            <类机器人:名字=android.intent.category.BROWSABLE/>
            <类机器人:名字=android.intent.category.APP_BROWSER/>
        &所述; /意图滤光器>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.VIEW/>            <类机器人:名字=android.intent.category.DEFAULT/>
            <类机器人:名字=android.intent.category.BROWSABLE/>            <数据机器人:计划=HTTP/>
            <数据机器人:计划=HTTPS/>
            <数据机器人:计划=约/>
            <数据机器人:计划=JavaScript的/>
        &所述; /意图滤光器>        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.VIEW/>            <类机器人:名字=android.intent.category.BROWSABLE/>
            <类机器人:名字=android.intent.category.DEFAULT/>            <数据机器人:计划=HTTP/>
            <数据机器人:计划=HTTPS/>
            <数据机器人:计划=内联/>
            <数据机器人:mime类型=text / html的/>
            <数据机器人:mime类型=text / plain的/>
            <数据机器人:mime类型=应用程序/ xhtml + xml/>
            <数据机器人:mime类型=应用/ vnd.wap.xhtml + XML/>
        &所述; /意图滤光器>
        <! - 要查看保存的网页存档。 - >
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.VIEW/>            <类机器人:名字=android.intent.category.BROWSABLE/>
            <类机器人:名字=android.intent.category.DEFAULT/>            <数据机器人:计划=HTTP/>
            <数据机器人:计划=HTTPS/>
            <数据机器人:计划=文件/>
            <数据机器人:mime类型=应用程序/ x-webarchive-XML/>
        &所述; /意图滤光器>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.WEB_SEARCH/>            <类机器人:名字=android.intent.category.DEFAULT/>
            <类机器人:名字=android.intent.category.BROWSABLE/>            <数据机器人:计划=/>
            <数据机器人:计划=HTTP/>
            <数据机器人:计划=HTTPS/>
        &所述; /意图滤光器>

LOG

  02-11 17:50:48.678:E / AndroidRuntime(29023):致命异常:主要
02-11 17:50:48.678:E / AndroidRuntime(29023):工艺:com.air.swiftmp,PID:29023
02-11 17:50:48.678:E / AndroidRuntime(29023):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.air.swiftmp / com.air.swiftmp.MainActivity}:显示java.lang.NullPointerException
02-11 17:50:48.678:E / AndroidRuntime(29023):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
02-11 17:50:48.678:E / AndroidRuntime(29023):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
02-11 17:50:48.678:E / AndroidRuntime(29023):在android.app.ActivityThread.access $ 800(ActivityThread.java:163)
02-11 17:50:48.678:E / AndroidRuntime(29023):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1257)
02-11 17:50:48.678:E / AndroidRuntime(29023):在android.os.Handler.dispatchMessage(Handler.java:102)
02-11 17:50:48.678:E / AndroidRuntime(29023):在android.os.Looper.loop(Looper.java:157)
02-11 17:50:48.678:E / AndroidRuntime(29023):在android.app.ActivityThread.main(ActivityThread.java:5335)
02-11 17:50:48.678:E / AndroidRuntime(29023):在java.lang.reflect.Method.invokeNative(本机方法)
02-11 17:50:48.678:E / AndroidRuntime(29023):在java.lang.reflect.Method.invoke(Method.java:515)
02-11 17:50:48.678:E / AndroidRuntime(29023):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1265)
02-11 17:50:48.678:E / AndroidRuntime(29023):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
02-11 17:50:48.678:E / AndroidRuntime(29023):在dalvik.system.NativeStart.main(本机方法)
02-11 17:50:48.678:E / AndroidRuntime(29023):致:显示java.lang.NullPointerException
02-11 17:50:48.678:E / AndroidRuntime(29023):在com.air.swiftmp.MainActivity.onCreate(MainActivity.java:95)
02-11 17:50:48.678:E / AndroidRuntime(29023):在android.app.Activity.performCreate(Activity.java:5389)
02-11 17:50:48.678:E / AndroidRuntime(29023):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-11 17:50:48.678:E / AndroidRuntime(29023):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)


解决方案

我会开门见山,如果


  

它所做的就是打开我的应用程序,并粘贴在文本字段中的URL。


这将意味着

 如果(Intent.ACTION_MAIN.equals(getIntent()的getAction())){

返回true

我假设你可浏览的意图也是应用程序的主要目的。

那么,你的'如果'做的是检查,如果你的意图(这也是您浏览的)是主要意图,这将是真实的。

编辑:

好吧,基本上你检查,如果你的正在打开浏览的屏幕是当它是双头呆您的应用程序应该显示在屏幕上。

在您的Andr​​oid清单你必须像

 <意向滤光器>
     <作用机器人:名字=android.intent.action.MAIN/>
     <类机器人:名字=android.intent.category.BROWSABLE/>
 &所述; /意图滤光器>

这是您浏览的意图。
当你调用getIntent()的getAction(),它会返回相同的值Intent.MAIN_ACTION。那是因为你在行动在清单中定义它。没有错。

 <作用机器人:名字=android.intent.action.MAIN/>

其次要加载屏幕打开时,您的应用程序获取URL。
我没有任何这方面的经验,但我强烈建议看链接,因为它似乎对加载在你的应用程序的URL与网页视图来解释。

我希望这为您提供了足够的答案和解决一些困惑!

EDIT2:

我要猜你havnt,无论是初始化的网页视图,或havnt设置内容查看web视图上

所以试图取代你的

 的setContentView(小于Arbitary事情指的很可能是一个布局>);

 的WebView的WebView =新的WebView(本);
 的setContentView(web视图);

既然你要指的是另一个布局,则无法加载XML你的TextView的,所以我建议删除

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

和其他地方你可以参考URI。

我也建议阅读链接的例子,因为它看起来像一个良好的开端。其他然后是,仅仅尝​​试和谷歌的WebView教程。

和看你的日志,在线95你MainActivity东西返回null。如果我认为行不通,你能告诉我什么是在线95?

EDIT3:

 公共类MainActivity延伸活动{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
       super.onCreate(savedInstanceState);
       的WebView的WebView =新的WebView(本);
       的setContentView(web视图);       webView.loadUrl(http://www.google.com);    }
}

和添加到您的Andr​​oid清单只是标签下方

 <使用许可权的android:NAME =android.permission.INTERNET对/>

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

既然你是新到Android让我解释什么是最有可能是造成你的错误:

当你做

 的setContentView(R.id.layout_name);

R.id 的会看在做出任何引用的 layout_name.xml 的是由您的setContentView设置。所以,如果你有 layout_name_2.xml 的与假设的 R.id.urlField_2 的,你尝试做

  TextView的URI =(的TextView)findViewById(R.id.urlField2);

它将找不到R.id.urlField2,因为它不是在LAYOUT_NAME限定。活动将只在看的的setContentView()中指定的布局的。有办法的布局结合起来,所以你可以使用多个XML的1活动里面,但这是另一天的话题。

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.

这篇关于从浏览的意图机器人负载网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆