在Activity里面开启和关闭后台服务
startService(new Intent(this, MyService.class));
stopService(new Intent(this, MyService.class));
在manifest的application里面声明服务
<service android:enabled="true" android:name=".MyService"/>
继承Service实现自己的服务类
public class MyService extends Service {
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
-
- }
- @Override
- public void onDestroy() {
-
- }
- @Override
- public void onStart(Intent intent, int startid) {
-
- }
- }