我创建了 Kivy android 应用程序,并希望通过点击此处和此处的链接创建提醒服务以在特定时间显示通知。当我启动我的应用程序时该服务成功运行,也可以在从最近的应用程序清除后自动重新启动。但是有一些问题我无法解决,希望这里的任何人都可以提供帮助。
E AndroidRuntime: java.lang.RuntimeException: Unable to start service com.example.test.ServiceMyservice@18ea0a6 with Intent { flg=0x10000000 cmp=com.example.test/.ServiceMyservice (has extras) }: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
这是我的代码。/service/myservice.py 中的服务文件
import datetime
from plyer import notification
import schedule
import time
def noti():
notification.notify(title=datetime.datetime.now(), message="test", app_name='test', timeout=3600, ticker='', toast=False)
class KivyService:
def __init__(self):
pass
def start(self):
while True:
schedule.run_pending()
time.sleep(1)
runjob = schedule.get_jobs('reminder')
if not runjob:
schedule.every(5).seconds.do(noti).tag("reminder")
if __name__ == '__main__':
service = KivyService()
service.start()
以下条目添加到 AndroidManifest.tmpl.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
另外,在下面添加之前
<receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
和 MyBroadcastReceiver.java
package com.example.test;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import com.example.test.ServiceMyservice;
public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String package_root = context.getFilesDir().getAbsolutePath();
String app_root = package_root + "/app";
Intent ix = new Intent(context, ServiceMyservice.class);
ix.putExtra("androidPrivate", package_root);
ix.putExtra("androidArgument", app_root);
ix.putExtra("serviceEntrypoint", "./service/myservice.py");
ix.putExtra("pythonName", "myservice");
ix.putExtra("pythonHome", app_root);
ix.putExtra("pythonPath", package_root);
ix.putExtra("pythonServiceArgument", app_root+":"+app_root+"/lib");
ix.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(ix);
}
}
该示例使用 ix.putExtra("serviceEntrypoint", "./service/main.py"); 我不确定它是否应该指向我的服务 python 文件,但我遇到了同样的错误。
还有我的 buildozer.spec 权限部分
android.permissions = INTERNET, ACCESS_NETWORK_STATE, RECEIVE_BOOT_COMPLETED, FOREGROUND_SERVICE
服务
services=myservice:service/myservice.py
谢谢。
如果有人遇到同样的问题,请回答我自己的问题。参考 PythonService.java 第 77 行,它需要一个参数 serviceStartAsForeground 但在我的参考链接中缺失,可能是由于版本差异。
boolean serviceStartAsForeground = (
extras.getString("serviceStartAsForeground").equals("true")
);
所以我需要在 Java 中添加以下行,无论是 true 还是 false,然后服务才能在重新启动后启动。
ix.putExtra("serviceStartAsForeground", "false");
但是,后台服务将在数小时后被系统终止以释放资源。仍在寻找一种方法使其注册到android警报管理器以重复任务,例如调用plyer通知并能够在重新启动后再次重新注册。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句