Jack Casas
Jack Casas

Reputation: 994

Launch Android App from WEB browser

I'm working on an internal app for my Company. Using Android Studio 2.2.3 as development environment, and AVD Emulator with Android 6.0 virtual device for testing.

We have a Web App that we use to create sales invoices. The Android App basically needs to receive an Invoice number, then it reads the Invoice data from a Web Service, and prints to a Bluetooth printer.

So basically, I've tried this 2 links in my web app:

<a href="company.printapp://INVOICE/1621696">PRINT INVOICE</a></div>
<a href="intent://INVOICE/1621696/#Intent;scheme=company.printapp;end">PRINT INVOICE</a>

Both throw the same error in the browser (testing in the emulator):

net:ERR_UNKNOWN_URL_SCHEME

My Manifest file looks like this:

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

Is there a solution that will work in both Chrome and Android's default web Browser? If not, I need it to work at least with Chrome.

Upvotes: 4

Views: 8582

Answers (1)

Jack Casas
Jack Casas

Reputation: 994

Guess I got my own answer after some more research:

It only works in Google Chrome but does the job in my case:

Links should look like:

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

In the AndroidManifest.xml the important part is:

...

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>

And finally this is how I get the parameters I pass in the Intent link (doctype and 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);
        }
    }

Upvotes: 12

Related Questions