首页 > 解决方案 > 如何使用 kivy 在 Android 上拦截来电?

问题描述

如何使用 kivy 在 Android 上拦截来电?

我在java上找到的

https://github.com/Newbilius/android_custom_call_info/blob/master/src/com/caller/info/CallReceiver.java

我还发现了这个http://5.9.10.113/42330739/listner-about-listening-incoming-calls-python-pyjnius,它是用 pyjnius 写的,但它不起作用

我正在尝试这个进行测试:

CallReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;


public class CallReceiver extends BroadcastReceiver {
    private static boolean incomingCall = false;
    private static WindowManager windowManager;
    private static ViewGroup windowLayout;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
            String phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            if (phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                //
                /*
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ie) {
                    //ну и ладно
                }*/
                String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                incomingCall = true;
                Log.debug("Show window: " + phoneNumber);
                showWindow(context, phoneNumber);

            } else if (phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

                if (incomingCall) {
                    Log.debug("Close window.");
                    closeWindow();
                    incomingCall = false;
                }
            } else if (phoneState.equals(TelephonyManager.EXTRA_STATE_IDLE)) {

                if (incomingCall) {
                    Log.debug("Close window.");
                    closeWindow();
                    incomingCall = false;
                }
            }
        }
    }
  }

主文件

from jnius import autoclass

CallReceiver = autoclass('CallReceiver')
CallReceiver.onReceive(True)

并在规范文件中添加 CallReceiver.java。但这不是用 buildozer 构建的

UPD

好的,我得到了有关呼叫的信息(还没有号码)

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):
        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))
        simState = telephonyManager.getCallState()
        print(simState)
        if simState == 1:
            print('CALLING!!!')
        else:
            print('NOT CALLING!!!')


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

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

if __name__ == '__main__':
    callreceiver = CallReceiver()
    while True:
        callreceiver.build()
        time.sleep(3)

但它可以在一台装有 Android 10 的设备上运行,而不能在两台装有 6 和 8 android 的设备上运行。在 logcat 我看到

W/SDL: Request to get environment variables before JNI is ready
I/python:  Traceback (most recent call last):
I/python:    File "/home/nickfaces/PycharmProjects/OnlineBuilds/.buildozer/android/app/service/main.py", line 48, in <module>
I/python:    File "/home/nickfaces/PycharmProjects/OnlineBuilds/.buildozer/android/app/service/main.py", line 14, in build
I/python:    File "/home/nickfaces/PycharmProjects/OnlineBuilds/.buildozer/android/app/service/main.py", line 23, in on_broadcast
I/python:    File "jnius/jnius_export_class.pxi", line 755, in jnius.jnius.JavaMethod.__call__
I/python:    File "jnius/jnius_export_class.pxi", line 699, in jnius.jnius.JavaMethod.ensure_method
I/python:  jnius.jnius.JavaException: Unable to find a None method!
I/python:  classname: None, definition: ()Landroid/os/Bundle;

而且我不知道如何获得电话号码

标签: pythonandroidkivypyjniuspython-for-android

解决方案


推荐阅读