Android開發(fā)服務(wù)Service全面講解
采用Service的方式,將Activity的計(jì)算的業(yè)務(wù)交給Service去做
參考代碼
1.目錄

2.布局文件
采用線性布局的方式
界面中主要包含的內(nèi)容有EditText兩個(gè)
三個(gè)Button按鈕
布局界面代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="輸入計(jì)算的值"
android:textSize="25dp"
android:singleLine="true"
android:maxLength="10"
/>
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="服務(wù)綁定"
android:textSize="25dp"
/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解除綁定"
android:textSize="25dp"
/>
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="求 和"
android:textSize="25dp"
/>
<EditText
android:focusable="false"
android:enabled="false"
android:focusableInTouchMode="false"
android:singleLine="true"
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="結(jié)果"
android:textSize="25dp"
/>
</LinearLayout>預(yù)覽圖:

3.創(chuàng)建MyService類
提供服務(wù)的類
需要繼承Service
因?yàn)椴捎玫氖墙壎ǚ?wù)的方式需要重寫create,bind,unbind以及destory生命周期方法
創(chuàng)建內(nèi)部對(duì)象繼承Bind用于再bind生命周期中返回用于提供服務(wù)實(shí)現(xiàn)交互
/*
* 需要進(jìn)行靜態(tài)注冊(cè)
* */
public class MyService extends Service {
//創(chuàng)建內(nèi)部類對(duì)象
MyBinder myBinder=new MyBinder();
@Override
public void onCreate() {
Toast.makeText(this, "創(chuàng)建服務(wù)!", Toast.LENGTH_SHORT).show();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(this, "綁定服務(wù)!", Toast.LENGTH_SHORT).show();
//返回的對(duì)象是內(nèi)部類對(duì)象
return myBinder;
}
@Override
public boolean onUnbind(Intent intent) {
Toast.makeText(this, "解綁服務(wù)!", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public void onDestroy() {
Toast.makeText(this, "銷毀服務(wù)!", Toast.LENGTH_SHORT).show();
}
//內(nèi)部類
class MyBinder extends Binder {
//返回Service對(duì)象
public MyService getService(){
return MyService.this;
}
//計(jì)算
//計(jì)算
public int getSum(int n){
int sum=0;
for(int i=1;i<=n;i++){
sum+=i;
}
return sum;
}
}
}4.Activity
控件的定義
事件監(jiān)聽器的創(chuàng)建
連接對(duì)象的創(chuàng)建,創(chuàng)建ServiceConnection對(duì)象,重寫服務(wù)創(chuàng)建的方法,獲取binder對(duì)象從而獲得Service對(duì)象
調(diào)用服務(wù)的時(shí)候和Activity跳轉(zhuǎn)的時(shí)候是類似的
調(diào)用Service服務(wù)的時(shí)候需要調(diào)用myBind就是調(diào)用了Service的內(nèi)部類
public class MainActivity extends Activity {
//定義控件
Button btn1 = null;//綁定
Button btn2 = null;//解綁
Button btn3 = null;//求和
EditText num;//計(jì)算數(shù)據(jù)
EditText result;//結(jié)果
MyService.MyBinder myBinder = null;
MyService myService = null;
//創(chuàng)建連接對(duì)象
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myBinder = (MyService.MyBinder) service;
myService = myBinder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取控件
initView();
//求和
getResultNum();
}
//初始化控件
public void initView() {
//獲取開啟服務(wù)按鈕
btn1 = (Button) findViewById(R.id.btn1);
//獲取停止服務(wù)按鈕
btn2 = (Button) findViewById(R.id.btn2);
//求和
btn3 = (Button) findViewById(R.id.btn3);
//獲取要計(jì)算的數(shù)據(jù)
num = findViewById(R.id.num);
// 獲取結(jié)果值
result = findViewById(R.id.result);
//調(diào)用點(diǎn)擊事件
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MyService.class);
intent.putExtra("num", num.getText().toString());
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
});
//解綁服務(wù)
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unbindService(conn);
}
});
}
private void getResultNum() {
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (num.getText().toString().length() == 0) {
result.setText("");
Toast.makeText(getApplicationContext(), "要計(jì)算的結(jié)果不能為空!", Toast.LENGTH_SHORT).show();
}
else if (num.getText().toString().matches("\\d*")==false){
result.setText("");
Toast.makeText(getApplicationContext(), "要計(jì)算的數(shù)據(jù)必須為數(shù)字!", Toast.LENGTH_SHORT).show();
}
else {
result.setText(myBinder.getSum(Integer.parseInt(num.getText().toString()))+"");
}
}
});
}
}效果圖
1.運(yùn)行界面

2.服務(wù)創(chuàng)建服務(wù)綁定
首先先進(jìn)行服務(wù)的創(chuàng)建再進(jìn)行服務(wù)的綁定


3.服務(wù)解綁和銷毀
解綁服務(wù)會(huì)調(diào)用Unbind生命周期,又因?yàn)闆]有別的可以解綁的服務(wù),所以就會(huì)調(diào)用Destory生命周期方法

到此這篇關(guān)于Android開發(fā)服務(wù)Service全面講解的文章就介紹到這了,更多相關(guān)Android Service內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android輸入框控件ClearEditText實(shí)現(xiàn)清除功能
這篇文章主要為大家詳細(xì)介紹了Android輸入框控件ClearEditText實(shí)現(xiàn)清除功能,感興趣的小伙伴們可以參考一下2016-05-05
Android編程調(diào)用系統(tǒng)自帶的拍照功能并返回JPG文件示例【附demo源碼下載】
這篇文章主要介紹了Android編程調(diào)用系統(tǒng)自帶的拍照功能并返回JPG文件,結(jié)合實(shí)例形式分析了Android的拍照功能調(diào)用及圖形文件操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-07-07
基于Android引入IjkPlayer無法播放mkv格式視頻的解決方法
下面小編就為大家分享一篇基于Android引入IjkPlayer無法播放mkv格式視頻的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
詳解Activity之singletast啟動(dòng)模式及如何使用intent傳值
在一個(gè)新棧中創(chuàng)建該Activity實(shí)例,并讓多個(gè)應(yīng)用共享改棧中的該Activity實(shí)例。一旦改模式的Activity的實(shí)例存在于某個(gè)棧中,任何應(yīng)用再激活改Activity時(shí)都會(huì)重用該棧中的實(shí)例,其效果相當(dāng)于多個(gè)應(yīng)用程序共享一個(gè)應(yīng)用,不管誰激活該Activity都會(huì)進(jìn)入同一個(gè)應(yīng)用中2015-11-11
使用Android原生WebView+Highcharts實(shí)現(xiàn)可左右滑動(dòng)的折線圖
折線圖是Android開發(fā)中經(jīng)常會(huì)碰到的效果,但由于涉及自定義View的知識(shí),對(duì)許多剛?cè)腴T的小白來說會(huì)覺得很高深,下面這篇文章主要給大家介紹了關(guān)于如何使用Android原生WebView+Highcharts實(shí)現(xiàn)可左右滑動(dòng)的折線圖的相關(guān)資料,需要的朋友可以參考下2022-05-05

