我试图在启动时启动我的kivy应用服务。在启动时启动Kivy服务(Android)
我确定我的服务没问题,因为它在我启动我的应用程序时起作用。但是在启动时我有一个问题。
我读过this article并试图使它:
package net.saband.myapp;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import org.kivy.android.PythonActivity;
public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent ix = new Intent(context, PythonActivity.class);
ix.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(ix);
}
}
它的工作原理,但启动应用程序,但不是服务。所以,我研究了StackOverflow的一些问题,并改变了我的代码如下:
package net.saband.myapp;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import net.saband.myapp.ServiceMyservice;
public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent ix = new Intent(context, ServiceMyservice.class);
ix.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(ix);
}
}
...并得到了一个错误:
10-21 19:16:44.784 1513 1569 I ActivityManager: Start proc 6334:net.saband.myapp:service_myservice/u0a116 for service net.saband.myapp/.ServiceMyservice
10-21 19:16:44.786 6334 6334 I art : Late-enabling -Xcheck:jni
10-21 19:16:44.885 6334 6334 D AndroidRuntime: Shutting down VM
10-21 19:16:44.888 6334 6334 E AndroidRuntime: FATAL EXCEPTION: main
10-21 19:16:44.888 6334 6334 E AndroidRuntime: Process: net.saband.myapp:service_myservice, PID: 6334
10-21 19:16:44.888 6334 6334 E AndroidRuntime: Theme: themes:{}
10-21 19:16:44.888 6334 6334 E AndroidRuntime: java.lang.RuntimeException: Unable to start service [email protected] with Intent { cmp=net.saband.myapp/.ServiceMyservice }: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
能否请您给我解释一下什么是错的,我该怎么办开始服务?谢谢!
修订
通过@Juggernaut请求添加我的服务代码:
from time import sleep
if __name__ == '__main__':
while True:
print "myapp service"
sleep(5)
它,当我运行的应用程序的工作原理,因为应用程序调用该服务:
def __start_service(self):
if platform == 'android':
service = autoclass('net.saband.myapp.ServiceMyservice')
mActivity = autoclass('org.kivy.android.PythonActivity').mActivity
argument = ''
service.start(mActivity, argument)
已更新(AndroidManifest)
以下是我的AndroidManifest.xml中的一些字符串。
我有项值权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
我有接收器:
<receiver android:name=".MyBroadcastReceiver" android:enabled="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>
我已经注册该服务:
<service android:name="net.saband.myapp.ServiceMyservice" android:process=":service_myservice" />
通过@mariachi的意见,我已经试图改变android:enabled="true" to android:enabled="false"
在接收器,并添加android:exported="false"
的服务。在这种情况下,设备启动时什么也不发生:没有错误,没有服务。
https://stackoverflow.com/a/32033021/6764079 – Juggernaut
@Juggernaut感谢你的意见,但我不明白它是如何能帮助我。我试图检查ix.getExtras(),是的它是空的,但我不明白为什么,不明白我应该在这里做什么? – saband
也许你应该与我们分享你的服务代码 – Juggernaut