我需要从活动开始一个Android服务,并从其他活动停止该活动并停止服务。
任何帮助将不胜感激。
任何帮助将不胜感激。
服务框架:
public class MyService extends Service {
public static final String TAG = "MyServiceTag";
...
}
这是起始活动的一部分:
processStartService(MyService.TAG);
private void processStartService(final String tag) {
Intent intent = new Intent(getApplicationContext(), MyService.class);
intent.addCategory(tag);
startService(intent);
}
这是停止活动的一部分:
processStopService(MyService.TAG);
private void processStopService(final String tag) {
Intent intent = new Intent(getApplicationContext(), MyService.class);
intent.addCategory(tag);
stopService(intent);
}
如何启动/停止Android服务?
startService(intent)
或 stopService(intent)
这些函数可以从任何活动中调用。
如何停止一个活动? - 我理解为您需要离开此活动。 函数调用
finish()
将从活动堆栈中删除该活动。