安卓开发学习18-3:Service应用:Bound Service基本用法和生命周期

实例

1、需求:使用Bound Service实现随机筛选多个数字
在这里插入图片描述
2、Myservice.java

package com.example.service_page;

import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MyService extends Service {

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // 返回binder实例
        return new MyBinder();
    }

    //    Service被创建的时候调用
    @Override
    public void onCreate() {
        super.onCreate();
    }

//  创建内部binder类
    public class MyBinder extends Binder{
//      获取当前Service实例
        public MyService getService(){

            return MyService.this;

        }

    }

//  筛选随机数
    public List<String> listRandomNum(){

        int randomNum ;
        String result = new String();
        List<String> resultList = new ArrayList();

        for(int i = 0 ; i < 4 ; i++){

            randomNum = new Random().nextInt(33)+1;

            if(randomNum < 10){
                result = "0"+randomNum;
            }else{
                result = ""+randomNum;
            }

            resultList.add(result);
        }

        return resultList;

    }
}

3、MainActivity.java

package com.example.service_page;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    MyService myService = null;
    Intent intent = null;

    int[] textViews = new int[]{R.id.num1, R.id.num2,R.id.num3,R.id.num4};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button startSelect = findViewById(R.id.startSelect);

        startSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                List<String> list = myService.listRandomNum();


                for(int i = 0; i < list.size() ; i++){
                    TextView textView = findViewById(textViews[i]);
                    textView.setText(list.get(i));
                }

            }
        });

    }

//  Service连接对象
    private ServiceConnection serviceConnection = new ServiceConnection() {

//  Service与组件完成绑定调用的方法
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

//      获取Service对象, iBinder是在onBind对象中进行返回
        myService = ((MyService.MyBinder)iBinder).getService();

    }


//  Service与组件完成解除绑定调用的方法
    @Override
    public void onServiceDisconnected(ComponentName componentName) {

    }
};

    @Override
    protected void onStart() {
        super.onStart();
//      启动绑定Service并
        intent = new Intent(MainActivity.this, MyService.class);
        bindService(intent, serviceConnection, BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
//      取消绑定
        unbindService(serviceConnection);
    }
}

4、activity-main.xml

<?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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="4"
        >
        <TextView
            android:gravity="center"
            android:id="@+id/num1"
            android:layout_columnWeight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ></TextView>
        <TextView
            android:gravity="center"
            android:id="@+id/num2"
            android:layout_columnWeight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ></TextView>
        <TextView
            android:gravity="center"
            android:id="@+id/num3"
            android:layout_columnWeight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ></TextView>
        <TextView
            android:gravity="center"
            android:id="@+id/num4"
            android:layout_columnWeight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ></TextView>
    </GridLayout>

    <Button
        android:id="@+id/startSelect"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="随机筛选"
        ></Button>
</LinearLayout>

生命周期

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值