python - 如何使用 kivy 在 Android 上拦截来电?
问题描述
如何使用 kivy 在 Android 上拦截来电?
我在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;
而且我不知道如何获得电话号码
解决方案
推荐阅读
- asp.net-core - Windows Server 2008 R2 上的 ASP.NET Core 3.1
- php - 如何在 PHP FTP 模式下做相当于 PHP 的 is_dir?
- azure - Azure 函数中的 Azure Redis 触发器
- wordpress - 隐藏 ACF 选项子菜单项
- connect - 如何通过 Consul Connect 将 Wordpress 服务连接到 MariaDB 服务?
- git - 如何在当前分支上获取/重新设置特定分支?
- javascript - 使用按钮在表单中的 WTForms 中添加带有文本和选项的字段
- html - 更改 CSS :before/:hover 以包含以前的 div
- c - 如何使结构内的指针指向另一个指针指向的相同字符串?
- google-apps-script - 清除第二天表单条目上的脚本错误