Java 在android上用python kivy关闭应用程序后,如何使服务继续工作

Java 在android上用python kivy关闭应用程序后,如何使服务继续工作,java,python,android,service,pyjnius,Java,Python,Android,Service,Pyjnius,我想在关闭应用程序后,我的服务继续工作,但我不能这样做。我听说我应该使用startForeground(),但是如何在python中使用它呢?应用程序代码: from kivy.app import App from kivy.uix.floatlayout import FloatLayout from jnius import autoclass from kivy.uix.label import Label class MyApp(App): def build(self):

我想在关闭应用程序后,我的服务继续工作,但我不能这样做。我听说我应该使用
startForeground()
,但是如何在python中使用它呢?应用程序代码:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from jnius import autoclass
from kivy.uix.label import Label
class MyApp(App):
    def build(self):
        fl = FloatLayout()
        try:
            service = autoclass('org.test.myapp.ServiceMyservice')
            mActivity = autoclass('org.kivy.android.PythonActivity').mActivity                                                                        
            service.start(mActivity, "")
        except Exception as error:
            fl.add_widget(Label(text=str(error), font_size=(40)))
        return fl
if __name__ == '__main__':
    MyApp().run()
my服务/main.py的代码

import pickle, socket, jnius

for x in range(5):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host = 'example-78945.portmap.host'
    port = 78945
    s.connect((host,port))
    s.send(('hello world').encode('utf-8'))
ServiceMyservice.java的代码

package org.test.myapp.ServiceMyservice;

import android.content.Intent;
import android.content.Context;
import org.kivy.android.PythonService;
import android.app.Notification;
import android.app.Service;

public class ServiceMyservice extends PythonService {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }
    protected int getServiceId() {
        return 1;
    }

    static public void start(Context ctx, String pythonServiceArgument) {
        Intent intent = new Intent(ctx, ServiceMyservice.class);
        String argument = ctx.getFilesDir().getAbsolutePath() + "/app";
        intent.putExtra("androidPrivate", ctx.getFilesDir().getAbsolutePath());
        intent.putExtra("androidArgument", argument);
        intent.putExtra("serviceTitle", "My Application");
        intent.putExtra("serviceDescription", "Myservice");                                                                     
        intent.putExtra("serviceEntrypoint", "./service/main.py");
        intent.putExtra("pythonName", "myservice");
        intent.putExtra("serviceStartAsForeground", true);
        intent.putExtra("pythonHome", argument);
        intent.putExtra("pythonPath", argument + ":" + argument + "/lib");
        intent.putExtra("pythonServiceArgument", pythonServiceArgument);
        ctx.startService(intent);
    }

    static public void stop(Context ctx) {
        Intent intent = new Intent(ctx, ServiceMyservice.class);
        ctx.stopService(intent);
    }
}
服务启动并运行,但在关闭应用程序后,服务也会关闭。如何修复它

这种解决方法基本上是让服务自动重启。这意味着您的服务将从头开始。是的,这是硬编码。

向服务模板文件中的start()方法添加字符串参数

我的观点是错误的。对于要传递给由ctx.startService()方法触发的onStartCommand()方法的活动,这将是额外的。然后将“autoRestartService”与该重启参数值放在一起

My.buildozer/android/platform/build-/dists//templates/Service.tmpl.java:

package{{args.package};
导入android.content.Intent;
导入android.content.Context;
导入org.kivy.android.PythonService;
公共类服务{{name | capitalize}}扩展了PythonService{
{%if sticky%}
@凌驾
public int startType(){
返回开始时间;
}
{%endif%}
@凌驾
受保护的整型getServiceId(){
返回{{service_id}};
}
/*将“restart”字符串参数添加到start()方法*/
静态公共void开始(上下文ctx、字符串pythonServiceArgument、字符串重新启动){
Intent Intent=newintent(ctx,Service{{name | capitalize}}.class);
字符串参数=ctx.getFilesDir().getAbsolutePath()+“/app”;
intent.putExtra(“androidPrivate”,ctx.getFilesDir().getAbsolutePath());
意图。putExtra(“雄辩”,论点);
intent.putExtra(“serviceTitle”,“{{args.name}}”);
intent.putExtra(“serviceDescription”,“{{name | capitalize}}”);
intent.putExtra(“serviceEntrypoint”,“{{entrypoint}}”);
intent.putExtra(“pythonName”,“{{name}”);
intent.putExtra(“serviceStartAsForeground”,“{{foreground | lower}”);
intent.putExtra(“pythonHome”,论点);
putExtra(“pythonPath”,argument+”:“+argument+”/lib”);
putExtra(“pythonServiceArgument”,pythonServiceArgument);

intent.putExtra(“自动重新启动服务”,重新启动);/*也许解释您提供的代码会有更好的帮助。+1用于链接references@abhivemp忘了提及,此答案已更新。谢谢,它对我有效,但我遇到了问题,可能你已经解决了。这与服务通知单击有关。如果应用仍在运行,它会将焦点返回到应用。Anotherr如果我通过从“最近使用的应用程序”列表中刷出应用程序来关闭应用程序,则服务将重新启动,当我单击其通知时,只会显示黑屏,而不会显示已打开的应用程序。在doStartForeground()方法中,我尝试了
contextIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
但没有帮助。如何实现这一点?
android.permissions = FOREGROUND_SERVICE
...

services = myservice:./path/to/your-service.py:foreground