【问题标题】:Launch Android App from WEB browser从 WEB 浏览器启动 Android 应用程序
【发布时间】:2017-01-07 16:58:19
【问题描述】:

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

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

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

<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 的默认网络浏览器的解决方案?如果没有,我需要它至少可以与 Chrome 一起使用。

【问题讨论】:

标签: android android-intent


【解决方案1】:

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

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

链接应如下所示:

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

【讨论】:

  • 唯一对我有用的。谢啦。你能在这里分享你的研究吗?
猜你喜欢
  • 1970-01-01
  • 2011-06-05
  • 2012-06-20
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
相关资源
最近更新 更多