2017-10-21 181 views
10

我試圖在啓動時啓動我的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中的一些字符串。

  1. 我有項值權限:

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

  2. 我有接收器:

    <receiver android:name=".MyBroadcastReceiver" android:enabled="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>

  3. 我已經註冊該服務:

    <service android:name="net.saband.myapp.ServiceMyservice" android:process=":service_myservice" />

通過@mariachi的意見,我已經試圖改變android:enabled="true" to android:enabled="false"在接收器,並添加android:exported="false"的服務。在這種情況下,設備啓動時什麼也不發生:沒有錯誤,沒有服務。

+0

https://stackoverflow.com/a/32033021/6764079 – Juggernaut

+0

@Juggernaut感謝你的意見,但我不明白它是如何能幫助我。我試圖檢查ix.getExtras(),是的它是空的,但我不明白爲什麼,不明白我應該在這裏做什麼? – saband

+0

也許你應該與我們分享你的服務代碼 – Juggernaut

回答

0

OMG!我通過解決my another issue with kivy service找到了解決方案。只需添加一些額外功能就足夠了。這裏是工作代碼:

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) { 
     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/main.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); 
    } 
} 
1

您似乎嘗試啓動的服務或活動未初始化。 你沒有粘貼你的AndroidManifest,所以我會寫它應該有的。

的第一件事是,你應該有權限添加到開機運行:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

第二件事是,你應該在清單定義你的接收器:

<receiver android:name=".MyBroadcastReceiver" 
       android:enabled="false"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"/> 
     </intent-filter> 
    </receiver> 

的第三件事是您應該定義您嘗試啓動的服務或活動,例如:

<activity android:name=".PythonActivity" /> 
    <service android:name=".ServiceMyservice" android:exported="false"/> 
+0

謝謝你的答案,但它沒有幫助。我已經更新了我的AndroidManifest問題,我已經嘗試在接收器中將'android:enabled =「true」'更改爲'android:enabled =「false」'並添加'android:exported =「虛假「的服務。但在這種情況下,當設備啓動時不會發生任何事情:沒有錯誤,沒有服務。 – saband

+0

啓動時沒有錯誤是您的應用的好兆頭。你可以在你的活動/服務起點放一個記錄器來檢查你是否收到來自BroadcastReceiver的任何東西? – mariachi

+0

當然。看起來像BroadcastReceiver沒有被調用或沒有收到任何東西,因爲我沒有看到我的Log.d消息,如果我使用我的版本('android:enabled =「true」',沒有'android:exported =「false」')我可以看到我的Log.d消息。 – saband