【发布时间】:2021-05-07 14:52:48
【问题描述】:
我创建了 Kivy android 应用,并希望通过链接 here 和 here 创建提醒服务以在特定时间显示通知。 当我启动我的应用程序时服务运行成功,也可以在从最近的应用程序中清除后自动重新启动。 但是有一些问题我无法解决,希望这里的任何人都可以提供帮助。
- 重启手机后我的服务无法加载。 logcat 有错误。
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
谢谢。
【问题讨论】:
标签: python android kivy android-service