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

3
我正在为公司开发内部应用程序。使用Android Studio 2.2.3作为开发环境,使用AVD模拟器和Android 6.0虚拟设备进行测试。
我们有一个Web应用程序,用于创建销售发票。Android应用程序基本上需要接收发票号码,然后从Web服务读取发票数据,并打印到蓝牙打印机上。
因此,我在我的Web应用程序中尝试了这两个链接:
<a href="company.printapp://INVOICE/1621696">PRINT INVOICE</a></div>
<a href="intent://INVOICE/1621696/#Intent;scheme=company.printapp;end">PRINT INVOICE</a>

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

net:ERR_UNKNOWN_URL_SCHEME

我的清单文件看起来像这样:

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

MainActivity.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);    
    }
}

有没有一种解决方案可以在Chrome和Android默认的Web浏览器中都适用?如果没有,我需要至少能在Chrome中使用。


这里还有另一种方式,我希望它适用于所有的网页浏览器... https://dev59.com/tWcs5IYBdhLWcg3wOBNC - user15039100
1个回答

10

经过更多的研究,我发现答案:

它只在Google Chrome中有效,但在我的情况下可以胜任:

链接应该看起来像:

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

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

最后,这是我如何获取通过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);
        }
    }

只有这个方法对我起作用了。谢谢你。你可以在这里分享一下你的研究吗? - rminaj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接