如何使用 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;
                }
            }
        }
    }
  }

main.py

from jnius import autoclass

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

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

更新

好的,我得到了关于打电话的信息(还没有号码)

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 安卓系统的两台设备。在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;

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)