Launching an Android Application from the Browser

前端 未结 2 631
抹茶落季
抹茶落季 2020-11-30 06:36

I have looked at several Stack Overflow questions with similar titles to mine. Each is answered, and the original author seems satisfied, but when I try to replicate their r

相关标签:
2条回答
  • 2020-11-30 06:47

    You cannot use MAIN -- URLs in Android are only VIEWed from the browser or WebView.

    Also, none of your Intent filters include the BROWSABLE category, so they will never work with the browser.

    It is not recommended to invent your own schemes. You can use a regular HTTP scheme, for some domain that you control. This sample project demonstrates this. In particular, you can follow the pattern shown by this 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:host="www.this-so-does-not-exist.com" android:scheme="http" />
    </intent-filter>
    

    replacing www.this-so-does-not-exist.com with something you own, and perhaps adding in a path to narrow the filter's scope. You can also see this technique used by the Barcode Scanner app from ZXing.

    0 讨论(0)
  • 2020-11-30 06:59

    Try this, if you want to start your application from browser I've done this yesterday

    1) On localhost make the html page

    <html>
    <head>
    </head>
    <body>
    <a href="yourownscheme://example.com/">Test link</a>
    </body>
    </html>
    

    2) make a class

       import org.xml.sax.Parser;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.TextView;
    
    public class URLHandler extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
    
    
        TextView uri=(TextView)findViewById(R.id.uri);
        TextView uri2=(TextView)findViewById(R.id.uri);
    
    
        if (Intent.ACTION_MAIN.equals(getIntent().getAction())) {
          String intentUri=(new Intent("yourownscheme://example.com/")).toUri(Intent.URI_INTENT_SCHEME).toString();
    
    
    
          uri.setText(intentUri);
    
        }
       }
      }
    

    3) The manifest is below

    <?xml version="1.0" encoding="utf-8"?>
    <manifest android:versionCode="1"
              android:versionName="1.0"
              package="yourpackage name"
              xmlns:android="http://schemas.android.com/apk/res/android">
    
      <supports-screens android:largeScreens="true"
                        android:normalScreens="true"
                        android:smallScreens="false" />
      <application android:icon="@drawable/cw"
                   android:label="@string/app_name">
    
    
    
          <activity
            android:name=".URLHandler"
            android:label="@string/app_name" 
            android:exported="true">
            <intent-filter>
    
                <data  android:scheme="yourownscheme://example.com/" />
    
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    
    
      </application>
    </manifest>
    

    4) When you installed you'r application on emulator, open browser and go to localhost tap the link and your application, if it's installed gonna launch, othervice it would be error

    0 讨论(0)
提交回复
热议问题