javaandroidkivypyjnius

How to get current activity correctly?


I need to access current activity of my app. I checked this official code

If i use this code below as documentation says:

PythonActivity = autoclass('org.renpy.android.PythonActivity')

i can't access activity and it returns error. So after searching i found this:

PythonActivity = autoclass('org.kivy.android.PythonActivity')

There is 2 function below. First one is opening webbrowser just like in documentation and IT WORKS:

from jnius import cast
from jnius import autoclass
PythonActivity = autoclass('org.kivy.android.PythonActivity')
Intent = autoclass('android.content.Intent')
Uri = autoclass('android.net.Uri')
intent = Intent()
intent.setAction(Intent.ACTION_VIEW)
intent.setData(Uri.parse('http://kivy.org'))
currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
currentActivity.startActivity(intent)

Now i need to recode this for my own script. I want to add new contact to Contacts. I got 'WRITE_CONTACTS' permission for that. in buildozer.spec:

android.permissions = WRITE_CONTACTS

But my own script returns me error. My function:

from jnius import autoclass,cast
PythonActivity=autoclass('org.kivy.android.PythonActivity')
Intent = autoclass('android.content.Intent')
JS=autoclass('java.lang.String')
intent=Intent()
intent.setAction(Intent.ACTION_INSERT)
intent.setType('ContactsContract.RawContacts.CONTENT_TYPE')
intent.putExtra('ContactsContract.Intents.Insert.NAME',JS('Example Name'))
intent.putExtra('ContactsContract.Intents.Insert.PHONE',JS('7777777777'))
currentActivity=cast('android.app.Activity',PythonActivity.mActivity)
currentActivity.startActivity(intent)

Log:

jnius.jnius.JavaException: JVM exception occurred: No Activity found to handle Intent { typ=ContactsContract.RawContacts.CONTENT_TYPE (has extras) } android.content.ActivityNotFoundException

I don't know that Is this error eccours because of activity or my wrong type selection. I would be glad if anyone help me to handle this. Like this contents on media is very few. So i hope like this topics let people learn quickly just like me.


Solution

  • FINALLY, I found solution to access activities and getting Contacts datas.

    Accessing correctly PythonActivity:

    PythonActivity = autoclass("org.kivy.android.PythonActivity")
    

    Accessing Contacts Java Class:

    Contacts = autoclass("android.provider.ContactsContract$Contacts")
    

    Accessing ContactsContract.CommonDataKinds.Phone Java Class (same):

    Kinds = autoclass("android.provider.ContactsContract$CommonDataKinds$Phone")
    

    Creating getContentResolver():

    cr = PythonActivity.mActivity.getContentResolver()
    

    Now we need to create 2 query. In first, we are going to find ID and need to use it in second query to find equal NUMBER. Also need to check is contact has phone number? too. After that we can loop second query to find number:

    name_number_dict= {}   # Name:Number  # Change as you wish..
    while (que1.moveToNext()):  #Query 1 Loop
        if que1.getString(que1.getColumnIndex(Contacts.HAS_PHONE_NUMBER)):  #If has number
            que2 = cr.query(Kinds.CONTENT_URI, None, Kinds.CONTACT_ID + " = " + que1.getString(que1.getColumnIndex(CONTACTS_ID)), None, None)  #Query 2 Loop
            while (que2.moveToNext()):
                name_number_dict[que1.getString(que1.getColumnIndex(Contacts.DISPLAY_NAME))] = que2.getString(que2.getColumnIndex(Kinds.NUMBER)  # Get datas and put in dic.
    

    If you need to check all ColumnNames of query, use query1.getColumnNames() >> Referance

    I hope it helps you guys who need to use this in Python>Kivy applications..