【发布时间】:2011-09-17 11:29:18
【问题描述】:
我需要从 Activity 启动一个 android 服务并停止该 Activity 并从其他 Activity 停止服务。
任何帮助将不胜感激
【问题讨论】:
我需要从 Activity 启动一个 android 服务并停止该 Activity 并从其他 Activity 停止服务。
任何帮助将不胜感激
【问题讨论】:
服务框架:
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()
将从活动堆栈中删除活动。
【讨论】: