我試圖在啓動時啓動我的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