Примечание: Мой вопрос не является дубликатом этих вопросов, потому что им уже 10 лет, и все такое. были изменены, и они работают по-другому:
- Уничтожит ли ОС Android мой IntentService при нормальных обстоятельствах?
- Служба Android останавливается Когда приложение закрыто
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.myapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
А это моя услуга:
package org.beeware.android;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import android.util.Log;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
import com.example.myapp.R;
public class MyService extends Service {
private static final String ChannelID = "MyServiceChannel";
private String mySrv = "MyService";
private int ONGOING_NOTIFICATION_ID = 1; // This cannot be 0. So 1 is a good candidate.
@Override
public void onCreate() {
Log.d(mySrv, "Service created.");
// Start Python in this process
if (!Python.isStarted()) {
Python.start(new AndroidPlatform(this));
}
// Create notification channel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
ChannelID, "My Service Channel", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
if (manager != null) { manager.createNotificationChannel(serviceChannel); }
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(mySrv, "Service started.");
// Put the service in a foreground state
Intent notifIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifIntent, PendingIntent.FLAG_IMMUTABLE);
Notification notif = new NotificationCompat.Builder(this, ChannelID)
.setContentTitle("My Service")
.setContentText("Service is running")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentIntent(pendingIntent)
.build();
startForeground(ONGOING_NOTIFICATION_ID, notif);
Log.d(mySrv, "Starting worker thread...");
Python py = Python.getInstance();
PyObject srv_thread = py.getModule("myapp.app");
srv_thread.callAttr("StartServer");
Log.d(mySrv, "Worker thread started.");
Toast.makeText(this, "Service started !", Toast.LENGTH_SHORT).show();
return START_STICKY;
}
@Override
public void onDestroy() {
Log.d(mySrv, "Stopping service thread...");
Python py = Python.getInstance();
PyObject srv_thread = py.getModule("myapp.app");
srv_thread.callAttr("StopServer");
Log.d(mySrv, "Service thread stopped.");
Toast.makeText(this, "Service stopped !", Toast.LENGTH_SHORT).show();
Log.d(mySrv, "Service destroyed.");
}
@Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}
}
Подробнее здесь: https://stackoverflow.com/questions/788 ... cumstances
Мобильная версия