2017-01-07 133 views
1

我正在爲我的公司開發一個內部應用程序。使用Android Studio 2.2.3作爲開發環境,並使用AVD Emulator與Android 6.0虛擬設備進行測試。從WEB瀏覽器啓動Android應用程序

我們有一個Web應用程序用於創建銷售發票。 Android應用程序基本上需要接收發票號碼,然後從Web服務讀取發票數據,並打印到藍牙打印機。

所以基本上,我已經試過這2個鏈接在我的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的默認網絡瀏覽器中使用的解決方案?如果沒有,我需要它至少與Chrome一起工作。

回答

1

想我弄了些更多的研究,我自己的答案:

它僅適用於谷歌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> 

最後,這是我獲取我在意圖鏈接(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); 
     } 
    } 
相關問題