user9313301
user9313301

Reputation:

How to launch app with qr code?

I'm making an android app for gym exercises and I need to open a specific exercise by scanning exercise-specific QR codes.

The application opens if the QR code is gymassistant:// but i need to launch a specific exercise.

Here's what code i add to manifest

        <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="gymassistant"/>
        </intent-filter>

What should the QR code contain and what should I write in my Activity / Manifest to be able to open a specific exercise?

Upvotes: 1

Views: 7085

Answers (2)

Zimba
Zimba

Reputation: 3681

The question seems to be asking to open an app when QR is scanned, and if app isn't installed, open a web page instead of opening some app in phone to scan QR Code.

You might be interested in a new feature called deep linking, which opens registered apps when a QR Image is scanned by phone camera, if you have Apple iOS 11. Otherwise, it'll open up a web browser to either a website or redirect to app store to download app (based on fallback setting included in QR).

For users with earlier operating systems, "open your QR code scanning app to scan." (as usual). For those who don't have a QR scanning app, they'd need to download one (to scan QR Codes), or upgrade to a device that supports a similar function to Deep Linking.

Either approach seems to have the same goal (to open an app, either by scanning QR with data or opened by user to scan data in QR).

Upvotes: 0

ilkengin
ilkengin

Reputation: 301

According to a related content on Android Developers Site and another related topic on the same site you should get data from the bundle in your activity and accordingly.

So, when you register a data like <data android:scheme="gymassistant"/> you are allowing other apps to open your app with such a link as gymassistant:// However, it is not restricting your app with the remaining part of the URI. For instance, your activity also starts with such a link as gymassistant://exercise1 or gymassistant://exercise2 etc.

What you need to do as I mentioned above is to read data in your activity and act accordingly. For example, below code is the simplest way to handle that. I am sure you will find much better ways :)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(...);

    // Get the intent that started this activity
    Intent intent = getIntent();
    // Get the original QR code data
    String data = intent.getData().toString();
    // Extract the exercise id and use it
    String dataArgs = data.replace("gymassistant://", "");
    switch(dataArgs) {
        case "exercise1":
            //show exercise1 related view
            break;
        case "exercise2":
            //show exercise2 related view
            break;
        //ETC
    }
}

I hope I could help you with your problem :)

Upvotes: 2

Related Questions