android 基础一 <Service>

1. 创建一个service
public class MyService extends Service {}
2.在清单中申明service
<application>
    <service
        android:name=".MyService"
        android:enabled="true"
        android:exported="true">
    </service>
</application>
3.调用service
Intent intent=new Intent(this,MyService.class);
this.startService(intent);
4.停止service
this.stopService(intent);或者 stopSelf
 
执行流程:
onCreate() //启动服务的时候执行,只执行一次
onStartCommand(Intent intent, int flags, int startId) //每次调用startService
onDestroy() //调用stoService 执行

猜你喜欢

转载自www.cnblogs.com/jtzp007/p/11478540.html