1. 创建一个Service的子类
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
2. 在manifest中配置Service
<service
android:name=".MyService"
android:enabled="true"
android:exported="false"/>
3. 启动与关闭Service
// 启动Service
Intent intent = new Intent(context, MyService.class);
context.startService(intent);
// 关闭Service
Intent intent = new Intent(context, MyService.class);
context.stopService(intent);
网友评论