2011-09-17 128 views

回答

-1

如何启动/停止的Android服务?

startService(intent)stopService(intent)

这些功能可以从任何活动被调用。

如何停止活动? - 由此我明白,你需要离开这个活动。函数调用

finish()

会从活动堆栈中删除的活动。

+0

即使在活动结束后,我仍然需要继续运行该服务,并且需要cl ose服务和其他活动 – RanjitRock

+0

我不是很确定如何停止此服务。也许你可以让一个静态服务实例从任何地方通过应用程序访问它。 – Shafi

25

服务骨架:

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); 
} 
相关问题