从 WEB 浏览器启动 Android 应用程序

Jac*_*sas 2 android android-intent

我正在为我的公司开发一个内部应用程序。使用Android Studio 2.2.3作为开发环境,使用带有Android 6.0虚拟设备的AVD Emulator进行测试。

我们有一个用于创建销售发票的 Web 应用程序。Android 应用程序基本上需要接收一个发票编号,然后它从 Web 服务读取发票数据,并打印到蓝牙打印机。

所以基本上,我已经在我的网络应用程序中尝试了这 2 个链接:

<a href="company.printapp://INVOICE/1621696">PRINT INVOICE</a></div>
<a href="intent://INVOICE/1621696/#Intent;scheme=company.printapp;end">PRINT INVOICE</a>
Run Code Online (Sandbox Code Playgroud)

两者都在浏览器中抛出相同的错误(在模拟器中测试):

net:ERR_UNKNOWN_URL_SCHEME
Run Code Online (Sandbox Code Playgroud)

我的清单文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="company.printapp">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <data android:scheme="company.printapp" />
                <action android:name="android.intent.category.BROWSABLE" />
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>
    </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

主活动.java:

package company.printapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.List;
import  android.net.Uri;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Uri data = getIntent().getData();
        String scheme = data.getScheme();
        String host = data.getHost();
        List<String> params = data.getPathSegments();
        String first = params.get(0); // "document type"
        String second = params.get(1); // "document No"
        BluetoothPrint(first,second);    
    }
}
Run Code Online (Sandbox Code Playgroud)

是否有适用于 Chrome 和 Android 的默认网络浏览器的解决方案?如果没有,我需要它至少与 Chrome 一起使用。

Jac*_*sas 6

猜猜我经过更多研究后得到了自己的答案:

它只适用于谷歌浏览器,但在我的情况下可以工作:

链接应如下所示:

<a href="intent://invoice/#Intent;scheme=company;package=company.printapp;S.doctype=FRA;S.docno=FRA1234;S.browser_fallback_url=http%3A%2F%2Fgoogle.com;end">PRINT INVOICE</a>
Run Code Online (Sandbox Code Playgroud)

在 AndroidManifest.xml 中,重要的部分是:

...

package="company.printapp"
...

<!-- Allow web apps to launch My App -->
            <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="company" android:host="invoice" android:path="/"/>
            </intent-filter>
Run Code Online (Sandbox Code Playgroud)

最后,这就是我如何获取在 Intent 链接中传递的参数(doctype 和 docno):

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bundle parametros = getIntent().getExtras();
        if (parametros != null){
            String Doctype = parametros.getString("doctype");
            String DocNo = parametros.getString("docno");
            TextView textView = (TextView) findViewById(R.id.DocNo);
            textView.setText(DocNo);
            BluetoothPrint(Doctype,DocNo);
        }
    }
Run Code Online (Sandbox Code Playgroud)