美文网首页
BinderService

BinderService

作者: 被罚站的树 | 来源:发表于2019-06-28 17:37 被阅读0次
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {
    BinderService binderService;//声名Service类对象

    int[] tvid={
      R.id.tv1,R.id.tv2,R.id.tv3,R.id.tv4,R.id.tv5,R.id.tv6,R.id.tv7
    };

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

        Button btn=findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List number=binderService.getNum();
                for(int i=0;i<7;i++) {
                    TextView tv = findViewById(tvid[i]);
                    tv.setText(number.get(i).toString());
                }
                Log.i("number",number.toString());
            }
        });

    }

    //创建ServiceConnection对象
    private ServiceConnection conn=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //获取后台Service
            binderService =((BinderService.MyBinder)service).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

    @Override
    protected void onStart() {//绑定Service
        super.onStart();
        Intent intent=new Intent(MainActivity.this,BinderService.class);
        bindService(intent,conn,BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {//解除绑定
        super.onDestroy();
        unbindService(conn);
    }
}

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

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

public class BinderService extends Service {
    public BinderService() {
    }

    //创建MyBinder内部类
    public class MyBinder extends Binder {

        public BinderService getService() {//创建获取Service方法
            return BinderService.this;//返回当前Service类
        }
    }



    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return new MyBinder();//返回MyBinder Service对象

    }
        //生成随机数的方法
    public List getNum(){

        List resArr=new ArrayList();
        String strNum="";//用于保存生成的随机数
        for (int i=0;i<7;i++){
            int num= new Random().nextInt(33)+1;//生成0-33的随机数
            if(num<10){
                strNum="0"+String.valueOf(num);
            }else {
                strNum=String.valueOf(num);
            }
            resArr.add(strNum);//把转换后的字符串添加点List集合中
        }
        return resArr;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}




相关文章

网友评论

      本文标题:BinderService

      本文链接:https://www.haomeiwen.com/subject/swmecctx.html