Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling unknown URI schemes in Android (Bitcoin example)

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.

like image 493
usr-local-ΕΨΗΕΛΩΝ Avatar asked Dec 14 '12 21:12

usr-local-ΕΨΗΕΛΩΝ


1 Answers

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.

like image 75
CommonsWare Avatar answered Oct 03 '22 07:10

CommonsWare