zoukankan      html  css  js  c++  java
  • 033 Android 绑定Service并与之通信

    1.绑定service

    2.实现方法

    3.在Androidmanifest.xml文件中配置service

    <service android:name=".Myservice"></service>

    4.java后台

    MainActivity界面

    package com.lucky.test38service2;
    
    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.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
        Button button_setbind;
        Button button_unsetbind;
        Button button_getvalue;
        Button button_clear;
        Intent intent;
        Myservice.Mybind mybind;       //定义一个Mybind
        ServiceConnection connection;  //定义一个ServiceConnection
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button_setbind=findViewById(R.id.button_setbind);
            button_unsetbind=findViewById(R.id.button_unsetbind);
            button_getvalue=findViewById(R.id.button_getvalue);
            button_clear=findViewById(R.id.button_clear);
            intent=new Intent(MainActivity.this,Myservice.class);
    
            //ServiceConnection 建立service连接
            connection=new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    mybind= (Myservice.Mybind) service;  //利用mybind作为MainActivity与Myservice信息交互的桥梁
                }
    
                @Override
                public void onServiceDisconnected(ComponentName name) {
    
                }
            };
    
            button_setbind.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    bindService(intent,connection,BIND_AUTO_CREATE); //绑定service
                }
            });
            button_unsetbind.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    unbindService(connection);  //解除service
                }
            });
            button_getvalue.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //显示service内容
                    Toast.makeText(MainActivity.this,mybind.getCount()+"",Toast.LENGTH_SHORT).show();
                }
            });
            button_clear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mybind.setCount(0);  //设置count值
                }
            });
        }
    }

    Myservice

    package com.lucky.test38service2;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    
    public class Myservice extends Service {
    
        boolean flag1;
        int count;
    
        //新建绑定工具类,绑定count
        public class Mybind extends Binder{
            //获取count值
            public int getCount(){
                return count;
            }
    
            //设置count值
            public void setCount(int arg){
                count=arg;
            }
        }
    
        Mybind mybind=new Mybind();
    
        @Override
        public IBinder onBind(Intent intent) {
            return mybind;
        }
    
        //service创建时调用的方法
        @Override
        public void onCreate() {
            super.onCreate();
            flag1=true;
            new Thread(){
                @Override
                public void run() {
                    while (flag1){
                        try {
                            Thread.sleep(1000);  //延时线程
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        count++;
                    }
                }
            }.start();
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            flag1=false;
        }
    
    
    }
  • 相关阅读:
    Using Repository and Unit of Work patterns with Entity Framework 4.0
    Accessing Your Model's Data from a Controller
    ASP.NET MVC 3 Internationalization
    Test your ASP.NET MVC or WebForms Application on IIS 7 in 30 seconds
    定时执行Web测试
    对包含HttpContext.Current.Cache的代码进行单元测试
    ASP.NET MVC判断基于Cookie的Session过期
    Selenium testing Iframe
    [Tips]: ASP.NET MVC得到controller的名字
    [Tips]:"RemoteOperationException: ERROR: wrong password for user" with Oracle 10g
  • 原文地址:https://www.cnblogs.com/luckyplj/p/10521863.html
Copyright © 2011-2022 走看看