Skip to content

Commit dc778b3

Browse files
committed
service.rsr auto-start section 2
1 parent deb259d commit dc778b3

File tree

1 file changed

+70
-2
lines changed

1 file changed

+70
-2
lines changed

doc/source/services.rst

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ The call to this method should be done within the service code::
134134
Service auto-start
135135
~~~~~~~~~~~~~~~~~~~~
136136

137-
For auto-start service on boot need add inside out AndroidManifest.xml this code::
137+
To automatically start the service on boot, you need to add signals inside ``AndroidManifest.xml`` that Android sends to applications on boot.
138+
Create file ``receivers.xml`` and write this code::
138139

139140
<receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true">
140141
<intent-filter>
@@ -144,6 +145,73 @@ For auto-start service on boot need add inside out AndroidManifest.xml this code
144145
</intent-filter>
145146
</receiver>
146147
147-
You can do it use section ``android.extra_manifest_application_xml`` in ``buildozer.spec`` code::
148+
149+
Next step set path to this file in ``buildozer.spec``, set setting ``android.extra_manifest_application_xml`` code::
148150

149151
android.extra_manifest_application_xml = %(source.dir)s/xml/receivers.xml
152+
153+
Then need create ``MyBroadcastReceiver.java``, code::
154+
155+
package com.heattheatr.kivy_service_test;
156+
157+
import android.content.BroadcastReceiver;
158+
import android.content.Intent;
159+
import android.content.Context;
160+
import org.kivy.android.PythonActivity;
161+
162+
import java.lang.reflect.Method;
163+
164+
import com.heattheatr.kivy_service_test.ServiceTest;
165+
166+
public class MyBroadcastReceiver extends BroadcastReceiver {
167+
168+
public MyBroadcastReceiver() {
169+
170+
}
171+
172+
// Start app.
173+
public void start_app(Context context, Intent intent) {
174+
Intent ix = new Intent(context, PythonActivity.class);
175+
ix.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
176+
context.startActivity(ix);
177+
}
178+
179+
// Start service.
180+
public void service_start(Context context, Intent intent) {
181+
String package_root = context.getFilesDir().getAbsolutePath();
182+
String app_root = package_root + "/app";
183+
Intent ix = new Intent(context, ServiceTest.class);
184+
ix.putExtra("androidPrivate", package_root);
185+
ix.putExtra("androidArgument", app_root);
186+
ix.putExtra("serviceEntrypoint", "service.py");
187+
ix.putExtra("pythonName", "test");
188+
ix.putExtra("pythonHome", app_root);
189+
ix.putExtra("pythonPath", package_root);
190+
ix.putExtra("serviceStartAsForeground", "true");
191+
ix.putExtra("serviceTitle", "ServiceTest");
192+
ix.putExtra("serviceDescription", "ServiceTest");
193+
ix.putExtra("pythonServiceArgument", app_root + ":" + app_root + "/lib");
194+
ix.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
195+
context.startService(ix);
196+
}
197+
198+
// Stop service.
199+
public void service_stop(Context context, Intent intent) {
200+
Intent intent_stop = new Intent(context, ServiceTest.class);
201+
202+
context.stopService(intent_stop);
203+
}
204+
205+
// Sinals reciver.
206+
public void onReceive(Context context, Intent intent) {
207+
switch (intent.getAction()) {
208+
case Intent.ACTION_BOOT_COMPLETED:
209+
System.out.println("python MyBroadcastReceiver.java
210+
MyBroadcastReceiver.class onReceive.method: ACTION_BOOT_COMPLETED");
211+
this.service_start(context, intent);
212+
break;
213+
default:
214+
break;
215+
}
216+
}
217+
}

0 commit comments

Comments
 (0)