Kivy Android Intent Filters

Intent filters are Android’s equivalent of file associations.  When you click on a file in a file manager on Android it starts the associated application by using an intent.  Applications can support the emission of intents with different categories.  Other apps which have registered to receive intents of that category can then receive and process them.

In order to make use of these intents from Kivy/buildozer you need to add an intent-filter to your a file (by default called intent_filters.xml) in your app’s root directory.  You also need to edit the buildozer.spec file to point to the file (look for this line and uncomment):

# (str) XML file to include as an intent filters in <activity> tag

The intent filter will differ depending on the intent.  Intents based on mimetypes are better than those based on paths.  Apparently there’s a problem with Android’s file search algorithm so you need to specify every relevant directory level if you want to filter on the file’s extension (seriously!).  Sample intent (from my art gridder app, which uses files with a .bag extension:

<action android:name=”android.intent.action.VIEW” />
<category android:name=”android.intent.category.DEFAULT” />
<data android:mimeType=”*/*” />
<data android:scheme=”file” />
<data android:host=”*” />
<data android:port=”*” />
<data android:pathPattern=”.*..*..*..*..*.bag” />
<data android:pathPattern=”.*..*..*..*.bag” />
<data android:pathPattern=”.*..*..*.bag” />
<data android:pathPattern=”.*..*.bag” />
<data android:pathPattern=”.*.bag” />

Finally, your app needs to be able to receive and extract the intent (don’t expect to find this in the docs!):

if __name__ == "__main__":
    if platform=="android":
        from jnius import cast
        from jnius import autoclass
        import android
        import android.activity

        # test for an intent passed to us
        PythonActivity = autoclass('')
        activity = PythonActivity.mActivity
        intent = activity.getIntent()
        intent_data = intent.getData()
            file_uri= intent_data.toString()
        except AttributeError:
            file_uri = None

Then pass the file uri to the app when you instantiate it.


3 Responses to Kivy Android Intent Filters

  1. Camille RUFU says:

    Hello, I search a simple sample of python/kivy code in order to red NFC tag. Impossible to find a complete sample. I have object as ‘PythonActivity”, “NfcAdapter.getDefaultAdapter()”, “intent”, but after …. the blackhole. PythonActivity.mActivity.startActivity(intent) ? nfcAdapter.enableReaderMode ? Can you hep me ?

    • Jari says:

      Here is full working example, . I finally managed solve my problem to read NFC, thanks to Stackoverflow….
      I prepared that example from all pieces which was around internet.
      In buildozer you don’t need intent filter if you just read NFC when your program is active. Here is modifications for default buildozer file which I have:

      source.include_exts = py,png,jpg,kv,atlas,pyc
      # (list) Permissions
      android.permissions = NFC

      # (int) Android API to use
      android.api = 19

      # (int) Minimum API required
      android.minapi = 14

      # (int) Android SDK version to use
      android.sdk = 24

      # (str) Android NDK version to use
      android.ndk = 10d

      #android.manifest.intent_filters = nfc_filter.xml

  2. Jari says:

    Hi, I’m also stuck with python+kivy+buildozer+nfc. I was able to get NFC reading work if tag contains text, but in all other cases it doesn’t work. It would be great to get simple example how to to read NFC with all information (not only NDEF).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: