6

I would like to integrate a "Donate via Bitcoin" button in an Android application's PreferenceScreen.

There are a few Bitcoin clients for Android running around, and Bitcoin wiki defines a URI scheme that is supposed to be used for BTC payments.

I have tried the following code

findPreference(getString(R.string.preference_donateBitcoin)).setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                String url = getString(R.string.pref_donateBitcoin_uri);
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);
                return false;
            }
        });

withou a BTC installed on phone. I've tried to launch the intent with the following URI: bitcoin:19iSEgNkJnEUtNDuuTkkZrU44PVKYMVfhz?amount=1 expecting Android telling me that no handler is installed.

Instead I got an ActivityNotFoundException

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=bitcoin:19iSEgNkJnEUtNDuuTkkZrU44PVKYMVfhz?amount=1 }

My question is about correctly handling (read "best practices") URI schemes unknown to device.

  • How do I check that a certain URI scheme can be handled at least by one application? (if more, I suppose a choice screen)
  • With reference to Bitcoin but without reference to any specific client, what should the best Intent be when paying via Bitcoin? How to handle the case when no BTC client is installed?

[Edit]: the question is wrong because I messed up my phone backups and presumed the Bitcoin Wallet app was installed when it was not.

usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305

1 Answers1

4

How do I check that a certain URI scheme can be handled at least by one application? (if more, I suppose a choice screen)

Either:

  • just catch the ActivityNotFoundException, or

  • use PackageManager and resolveActivity() or queryIntentActivities() to see if there is anything matching your Intent

With reference to Bitcoin but without reference to that specific client, what should the best Intent be when paying via Bitcoin?

You would have to ask the authors of Bitcoin apps that question, or encourage them to adopt a Uri standard, if they have not done so already.

According to the manifest for the application you list, your Intent looks like it should work. At least, using AppXplore, I see an activity for ACTION_VIEW for Uri values with a scheme of bitcoin. Hence, you may wish to contact the developer of this app and see if you can determine precisely where you are going wrong with your integration.

How to handle the case when no BTC client is installed?

Offer to take the user to the Play Store to install some Bitcoin client you like, via a market: Uri.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Thanks! Please let us ignore the "contact the developer" part because I got confused :) and presumed the BTC wallet was installed on phone (instead it runs only on my tablet). After installing it from Market I successfully opened it with that URI scheme. The rest of your answer fits my question and will be the solution to my problem – usr-local-ΕΨΗΕΛΩΝ Dec 14 '12 at 22:20
  • 1
    @djechelon: `ActivityNotFoundException` is a standard `RuntimeException`. Wrapping your `startActivity()` call in a suitable `try`/`catch` block should work. http://stackoverflow.com/questions/8399065/how-to-handle-the-activitynotfoundexception – CommonsWare Dec 14 '12 at 22:37