簡體   English   中英

從可瀏覽的意圖android加載url

[英]load url from browsable intent android

我在瀏覽器應用程序中實現了“ <category android:name =“ android.intent.category.BROWSABLE” />”,我希望其他任何顯示該列表的瀏覽器都可以選擇要處理該鏈接的應用程序,但我可以正常工作從列表中啟動我的應用程序,但是它不會將該URL加載到我的應用程序中,它所做的只是打開我的應用程序並將URL粘貼到文本字段中。 有什么線索嗎?

編輯:我嘗試過將它粘貼后在我的webview中加載url,但是應用程序強制關閉

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());
        }
    //}

表現

            <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)

我會直接指出

它要做的就是打開我的應用程序,然后將URL粘貼到文本字段中。

那意味着

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

返回true

我假設您的可瀏覽意圖也是該應用程序的主要意圖。

因此,您的“如果”正在做的是檢查您的“意圖”(也是您的可瀏覽性)是否是“主要意圖”,這是正確的。

編輯:

好吧,基本上,您正在檢查正在打開的可瀏覽屏幕是否是您的應用在打開時應顯示的屏幕。

在您的Android清單中,您會看到類似

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

這是您可瀏覽的意圖。 當您調用getIntent()。getAction()時,它將返回與Intent.MAIN_ACTION相同的值。 那是因為您在清單中實際定義了它。 沒錯。

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

其次,您想加載屏幕打開時應用程序獲取的URL。 我對此沒有任何經驗,但是我強烈建議您看一下鏈接,因為它似乎可以解釋有關使用webViews在您的應用程序中加載URL的信息。

希望這為您提供了足夠的答案並解決了一些困惑!

EDIT2:

我猜你是不是已經初始化了webView,還是在webView上設置了contentView

所以嘗試更換你的

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

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

由於將引用其他布局,因此無法為TextView加載xml,因此建議刪除

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

您可能會提到uri。

我還建議您閱讀鏈接中提供的示例,因為它似乎是一個不錯的起點。 除此之外,只需嘗試和google一起獲取WebView教程即可。

在第95行,查看您的Log。在MainActivity中,某些返回null。 如果我的建議不起作用,您能告訴我第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");

    }
}

並將其添加到標簽下方的android清單中

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

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

由於您是Android新手,因此讓我解釋最有可能導致您的錯誤的原因:

當你做

setContentView(R.id.layout_name);

R.id所做的任何引用都看在這是由你的setContentView設置layout_name.xml。 因此,如果您有layout_name_2.xmlR.id.urlField_2並嘗試

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

找不到R.id.urlField2,因為它不是在您的layout_name中定義的。 該活動將僅查看setContentView()中指定的布局。 有多種組合布局的方法,因此您可以在1活動中使用多個xml,但這是另一天的主題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM