Get incoming number in kivy with pyjnius

384 Views Asked by At

I`m trying get to get incoming number in kivy with pyjnius.

My service/main.py

from android.broadcast import BroadcastReceiver
from jnius import autoclass, cast
import time


class CallReceiver:
    def build(self):
        context = autoclass('android.content.Context')
        intent = autoclass('android.content.Intent')

        self.br = BroadcastReceiver(self.on_broadcast, actions=['headset_plug'])
        self.br.start()
        self.on_broadcast(context, intent)

    def on_broadcast(self, context, intent):
        #intent = autoclass('android.content.Intent')
        mContext = autoclass('android.content.Context')
        pythonActivity = autoclass('org.kivy.android.PythonService')
        TelephonyManager = autoclass('android.telephony.TelephonyManager')
        telephonyManager = cast('android.telephony.TelephonyManager',
                                pythonActivity.mService.getSystemService(mContext.TELEPHONY_SERVICE))
        while True:
            simState = telephonyManager.getCallState()
            if simState == 1:
                String = autoclass('java.lang.String')
                phoneNumber = intent.getStringExtra(String('telephonyManager .EXTRA_INCOMING_NUMBER'))
                #print(phoneNumber)
                phoneNumber = intent.getStringExtra(String('telephonyManager .EXTRA_INCOMING_NUMBER'))

                print(phoneNumber)

                print(type(phoneNumber))
                print('CALLING!!!')
                #time.sleep(3)
            elif simState == 0:
                print('NOT CALLING!!!')
                #time.sleep(3)


    def on_pause(self):
        self.br.stop()
        return True

    def on_resume(self):
        self.br.start()


    

if __name__ == '__main__':
    callreceiver = CallReceiver()
    callreceiver.build()

I get incoming call state, but always "None" phone number. In log I see this:

I/python: None
    <class 'NoneType'>
    CALLING!!!

And why broadcast receiver dont work with action "phone_state", but work with "headset_plug" ?

0

There are 0 best solutions below