Question

I've been looking everywhere regarding implementing a barcode scanner into my application/ I've already included the code which'll enable the user to scan if the Barcode scanner is installed but I now need something to prompt the user to download the application if it isn't already installed? I followed an example to get me this far.

Here is my code

    package com.example.zxingscan;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class Main extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    HandleClick hc = new HandleClick();

    findViewById(R.id.butQR).setOnClickListener(hc);

  }

  private class HandleClick implements OnClickListener{

    public void onClick(View arg0) {

      Intent intent = new Intent("com.google.zxing.client.android.SCAN");

      switch(arg0.getId()){

        case R.id.butQR:

          intent.putExtra("SCAN_MODE", "QR_CODE_MODE");

        break;


      }
      startActivityForResult(intent, 0);    //Barcode Scanner to scan for us
    }
  }
  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
      TextView tvStatus=(TextView)findViewById(R.id.tvStatus);
      TextView tvResult=(TextView)findViewById(R.id.tvResult);
      if (resultCode == RESULT_OK) {
        tvStatus.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
        tvResult.setText(intent.getStringExtra("SCAN_RESULT"));
      } else if (resultCode == RESULT_CANCELED) {
        tvStatus.setText("Press a button to start a scan.");
        tvResult.setText("Scan cancelled.");
      }
    }
  }
}
Was it helpful?

Solution

First of all create a dialog that briefly explains the situation and that suggests to install the free ZXing scanner app from Google Play. Add a Cancel and an Install button. I'm not going to explain how to do that here. Just take a look at this AlertDialog.Builder tutorial for example.

Upon user confirmation (Install button click) you just need to start the following intent:

startActivity(new Intent(Intent.ACTION_VIEW,
    Uri.parse("market://details?id=com.google.zxing.client.android")));

market protocol Uris can be used to open app detail pages from the Google Play market. See this Android documentation page for more information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top