How to get current activity correctly?

402 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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..

2
On

I don't know the Python syntax, but I can tell you what the problem is:

You are missing the Intent ACTION. You need to set the ACTION in the Intent to ContactsContract.Intents.Insert.ACTION (which has the string value "android.intent.action.INSERT")

Also, it looks like you are sending Android constant names instead of the actual string values of these things, for example:

you do this: intent.setType('ContactsContract.RawContacts.CONTENT_TYPE')

which sets the type in the Intent to the string value "ContactsContract.RawContacts.CONTENT_TYPE", which you can see here:

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

but the type needs to be set to the Android constant ContactsContract.RawContacts.CONTENT_TYPE which has the string value "vnd.android.cursor.dir/raw_contact"

As I said, I don't know Python syntax, but you probably need to do something like this:

intent=Intent()
intent.setAction(ContactsContract.Intents.Insert.ACTION)
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE)

or, if Python supports it, Intent has a constructor that takes an ACTION as a parameter, so this might also work:

intent=Intent(ContactsContract.Intents.Insert.ACTION)
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE)

Let me know if any of this makes sense.