implement Context.stopService()

This commit is contained in:
Julian Winkler
2024-07-19 15:22:12 +02:00
parent 23e7d32924
commit 71bb6d96bd
2 changed files with 13 additions and 1 deletions

View File

@@ -10,6 +10,10 @@ public abstract class Service extends Context {
System.out.println("Service.onCreate() called");
}
public void onDestroy() {
System.out.println("Service.onDestroy() called");
}
public abstract IBinder onBind(Intent intent);
public abstract int onStartCommand(Intent intent, int flags, int startId);

View File

@@ -553,7 +553,15 @@ public class Context extends Object {
}
}
public boolean stopService(Intent intent) {return false;}
public boolean stopService(Intent intent) throws ClassNotFoundException {
Class<? extends Service> cls = Class.forName(intent.getComponent().getClassName()).asSubclass(Service.class);
Service service = runningServices.remove(cls);
if (service != null) {
service.onDestroy();
return true;
}
return false;
}
public void unbindService(ServiceConnection serviceConnection) {}