欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)服務Service全面講解

 更新時間:2023年02月09日 14:10:53   作者:簡單點了  
Android的服務是開發(fā)Android應用程序的重要組成部分。不同于活動Activity,服務是在后臺運行,服務沒有接口,生命周期也與活動Activity非常不同。通過使用服務我們可以實現(xiàn)一些后臺操作,比如想從遠程服務器加載一個網(wǎng)頁等,下面來看看詳細內(nèi)容,需要的朋友可以參考下

采用Service的方式,將Activity的計算的業(yè)務交給Service去做

參考代碼

1.目錄

2.布局文件

采用線性布局的方式

界面中主要包含的內(nèi)容有EditText兩個

三個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="輸入計算的值"
        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="服務綁定"
        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="結果"
        android:textSize="25dp"
        />
</LinearLayout>

預覽圖:

3.創(chuàng)建MyService類

提供服務的類

需要繼承Service

因為采用的是綁定服務的方式需要重寫create,bind,unbind以及destory生命周期方法

創(chuàng)建內(nèi)部對象繼承Bind用于再bind生命周期中返回用于提供服務實現(xiàn)交互

/*
* 需要進行靜態(tài)注冊
* */
public class MyService extends Service {
    //創(chuàng)建內(nèi)部類對象
    MyBinder myBinder=new MyBinder();
    @Override
    public void onCreate() {
        Toast.makeText(this, "創(chuàng)建服務!", Toast.LENGTH_SHORT).show();
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Toast.makeText(this, "綁定服務!", Toast.LENGTH_SHORT).show();
        //返回的對象是內(nèi)部類對象
        return myBinder;
    }
    @Override
    public boolean onUnbind(Intent intent) {
        Toast.makeText(this, "解綁服務!", Toast.LENGTH_SHORT).show();
        return true;
    }
    @Override
    public void onDestroy() {
        Toast.makeText(this, "銷毀服務!", Toast.LENGTH_SHORT).show();
    }
    //內(nèi)部類
    class MyBinder extends Binder {
        //返回Service對象
        public MyService getService(){
            return MyService.this;
        }
        //計算
        //計算
        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)建

連接對象的創(chuàng)建,創(chuàng)建ServiceConnection對象,重寫服務創(chuàng)建的方法,獲取binder對象從而獲得Service對象

調(diào)用服務的時候和Activity跳轉(zhuǎn)的時候是類似的

調(diào)用Service服務的時候需要調(diào)用myBind就是調(diào)用了Service的內(nèi)部類

public class MainActivity extends Activity {
    //定義控件
    Button btn1 = null;//綁定
    Button btn2 = null;//解綁
    Button btn3 = null;//求和
    EditText num;//計算數(shù)據(jù)
    EditText result;//結果
    MyService.MyBinder myBinder = null;
    MyService myService = null;
    //創(chuàng)建連接對象
    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() {
        //獲取開啟服務按鈕
        btn1 = (Button) findViewById(R.id.btn1);
        //獲取停止服務按鈕
        btn2 = (Button) findViewById(R.id.btn2);
        //求和
        btn3 = (Button) findViewById(R.id.btn3);
        //獲取要計算的數(shù)據(jù)
        num = findViewById(R.id.num);
        // 獲取結果值
        result = findViewById(R.id.result);
        //調(diào)用點擊事件
        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);
            }
        });
        //解綁服務
        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(), "要計算的結果不能為空!", Toast.LENGTH_SHORT).show();
                }
                else if (num.getText().toString().matches("\\d*")==false){
                    result.setText("");
                    Toast.makeText(getApplicationContext(), "要計算的數(shù)據(jù)必須為數(shù)字!", Toast.LENGTH_SHORT).show();
                }
                else {
                    result.setText(myBinder.getSum(Integer.parseInt(num.getText().toString()))+"");
                }
            }
        });
    }
}

效果圖

1.運行界面

2.服務創(chuàng)建服務綁定

首先先進行服務的創(chuàng)建再進行服務的綁定

3.服務解綁和銷毀

解綁服務會調(diào)用Unbind生命周期,又因為沒有別的可以解綁的服務,所以就會調(diào)用Destory生命周期方法

到此這篇關于Android開發(fā)服務Service全面講解的文章就介紹到這了,更多相關Android Service內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論