服務是android中實現程式後台執行的解決方案。
服務依賴於建立服務時所在的應用程式程序,當應用程式程序被關掉後,所有依賴於該程序的服務也會停止執行。
服務中的**也是執行在主線程中的。
通過android studio來建立服務
右鍵包名然後 new>service>service
建立時有兩個屬性可以勾選,
exported屬性表示其他程式訪問這個服務,enabled屬性用於表示是否啟用這個服務。
注意:每個服務都需要在androidmanifest.xml中進行註冊才能生效。android studio 已經幫我們建立好了。
這個是建立好的類: 關於onbind()方法,之後再活動和服務之間的通訊會用到
service 中常用的幾個方法:public class myservice extends service
@override
public ibinder onbind(intent intent)
首次啟啟動時會建立乙個service例項,並一次呼叫oncreate()和onstartcommand()方法進入執行狀態,如果再次呼叫startservice()啟動service,將不會再建立新的service物件,而是會重用前面建立的service物件,並呼叫onstartcommand()方法。intent startintent=new intent(mainactivity.this,myservice.class);
startservice(startintent);
另外也可以通過在服務中呼叫intent stoptintent=new intent(mainactivity.this,myservice.class);
stopservice(stoptintent);
stopself()
來關閉服務我們需要通過service中的 onbind()方法來實現通訊。
然後在mainactivity中需要修改的是:public class myservice extends service
class downloadbinder extends binder catch (interruptedexception e)
progress++;}}
}).start();
}public int getprogress()}}
建立serviceconnection匿名類和自定義的binder類,並通過向下轉型得到downloadbinder的例項
private myservice.downloadbinder downloadbinder;
//監聽活動和服務的連線狀況,如果成功則**onserviceconnected()
private serviceconnection connection=new serviceconnection()
@override
public void onservicedisconnected(componentname name)
};
但三個引數bind_auto_create,表示在活動和服務進行繫結之後自動建立服務,intent bindintent=new intent(mainactivity.this,myservice.class);
bindservice(bindintent,connection,bind_auto_create);
這樣會使服務中的oncreate()執行,但是startcommand()不會執行,
接下來我們就可以呼叫downloadbinder的相關方法進行操作,實現在活動中呼叫服務中方法
downloadbinder.getprogress()
注:服務可以跟整個應用程式中任意乙個activity進行繫結,並獲取binder例項。
注:來自 菜鳥教程
當我們對乙個服務即呼叫了startservice()方法,又呼叫了bindservice()方法服務的系統優先順序相對來說還是比較低的,所有當系統出現記憶體不足的情況下,就有可能會**掉正在後台執行的服務。當我們需要服務一直保持執行狀態時,就可以考慮使用前台服務。android 系統的機制是,乙個服務只要被啟動或者被繫結後,就會一直處於執行狀態,必須要兩種狀態同時不滿足,服務才會銷毀。
所以我們需要通過同時呼叫stopservice()和unbindservice()方法來銷毀這個服務。
前台服務和普通服務的最大區別就是,前台服務會在狀態列一直顯示乙個通知。常見的有(網易雲**、360...)
接著我們可以呼叫setprogress()方法來更新進度條@override
public void oncreate() ,0);
notificationbuilder= new notificationcompat.builder(this);
notificationbuilder.setcontenttitle("download") ; //標題
notificationbuilder.setwhen(system.currenttimemillis()); //通知被建立的時間
notificationbuilder.setsmallicon(r.mipmap.ic_launcher); //狀態列通知圖示
notificationbuilder.setprogress(100,1,false); //條形進度條顯示
notificationbuilder.setautocancel(true);
notificationbuilder.setcontentintent(pi); //點選通知事件
//傳送通知,並將服務變為前台服務
startforeground(1,notificationbuilder.build());
}
重點就是startforeground(),這個方法會將服務變為前台服務,並在系統狀態列中顯示出來。notificationbuilder.setprogress(100,progress,false);
notificationbuilder.setcontenttext(progress+"/100");
intentservice可以簡單地建立乙個非同步的、會自動停止的服務。
建立myintentservice
最後不要忘了在androidmanifest.xml中進行註冊服務public class myintentservice extends intentservice
@override
protected void onhandleintent(intent intent)
@override
public void ondestroy()
}
啟動服務:
intent intentservice=new intent(this,myintentservice.class);
startservice(intentservice);
注意:intentservice 執行完畢後是自動停止。
myintentservice 中必須呼叫父類的有參建構函式,否則會拋異常
Android四大元件服務 Service
public class myservice extends service 當服務第一次建立時呼叫 override public void oncreate 當服務銷毀時呼叫 override public void ondestroy 開始服務是呼叫 override public void ...
Service服務Android四大元件之一
關於service服務 學了一天的service還是懵懵懂懂的狀態,什麼是service呢?service是應用四大元件之一,簡稱服務 用於長時間需要操作的程序,service一般來說是後台程式,簡單點說大多數的服務都是使用者看不見的,但是在它確實執行在後台處理一些程序操作。它是android裡面乙...
四大元件(四) content provider
content provider相當於是程式與程式之間的介面。讓原本不可能通訊的程式,通過內容提供者這個橋梁變得可以通訊。定義乙個類 繼承 contentprovider public class backdoor extends contentprovider 註冊contentprovider ...