以下是示例代码:
在服务中:
public int onStartCommand(Intent intent, int flags, int startId) { // do some work here... return START_STICKY; }
在Activity中:
@Override protected void onDestroy() { super.onDestroy(); stopService(new Intent(this, MyForegroundService.class)); }
请注意,这只是一个例子。具体的实现可能需要根据您的服务和应用程序的特定要求进行更改。