android 粘性service,Android服务1 Service
四大組件全部要注冊
Service特點:用于在后臺耗時操作
1.
public class MyService extends Service {
public IBinder onBind(Intent intent) {}
public void onCreate() {}
public int onStartCommand(Intent intent, int flags, int startId) {????? ? //? ? 運行在主線程,開子線程做耗時操作
return 返回值;????? ? //????START_REDELIVER_INTENT:帶數(shù)據(jù)的粘性標記。系統(tǒng)把服務殺死,服務會再次啟動,并且會保留傳遞的數(shù)據(jù)。
//????START_STICKY:粘性標記(默認)。系統(tǒng)把服務殺死,服務會再次嘗試連接,intent會為null。
//????START_NOT_STICKY:非粘性標記。系統(tǒng)把服務殺死,服務不會再次啟動。
}
public void onDestroy() {}
在MyService中停止服務:stopSelf();
}
2.啟動與停止服務:
context.startService(new Intent(this, MyService.class));
context.stopService(new Intent(this, MyService.class));
總結
以上是生活随笔為你收集整理的android 粘性service,Android服务1 Service的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android官方夜间模式,Androi
- 下一篇: android助手专业版,开发助手专业版