启动时启动Kivy服务(Android)

时间:2017-10-21 20:46:55

标签: java android python kivy

我正试图在启动时启动我的kivy app服务。

我确信我的服务没问题,因为它在我启动应用程序时有效。但是在启动时我遇到了问题。

我已阅读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 net.saband.myapp.ServiceMyservice@8c96929 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)

当我运行app时它会起作用,因为app会调用该服务:

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. 我有RECEIVE_BOOT_COMPLETED权限:

    <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" />

  4. 根据@mariachi的建议,我尝试更改接收方中的android:enabled="true" to android:enabled="false"并将android:exported="false"添加到服务中。在这种情况下,设备启动时不会发生任何事情:没有错误,没有服务。

2 个答案:

答案 0 :(得分:1)

您尝试启动的服务或活动似乎未初始化。 你没有粘贴你的AndroidManifest,所以我会写下它应该有的东西。

首先,您应该在启动时添加权限:

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

第二件事是你应该在Manifest中定义你的接收器:

    <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"/>

答案 1 :(得分: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);
    }
}