zoukankan      html  css  js  c++  java
  • 开启服务、停止服务、绑定服务、取消绑定服务

    package com.pingyijinren.test;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    import android.util.Log;
    
    public class MyService extends Service {
        private DownloadBinder downloadBinder=new DownloadBinder();
        public MyService() {
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            //throw new UnsupportedOperationException("Not yet implemented");
            return downloadBinder;
        }
    
        @Override
        public void onCreate(){
            super.onCreate();
            Log.d("MainActivity","create");
        }
    
        @Override
        public int onStartCommand(Intent intent,int flags,int startId){
            Log.d("MainActivity","startCommand");
            return super.onStartCommand(intent,flags,startId);
        }
    
        @Override
        public void onDestroy(){
            super.onDestroy();
            Log.d("MainActivity","destroy");
        }
    
        class DownloadBinder extends Binder {
            public void startDownload(){
                Log.d("MainActivity","startDownload");
            }
    
            public int getProgress(){
                Log.d("MainActivity","getProgress");
                return 0;
            }
        }
    }
    package com.pingyijinren.test;
    
    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;
    
    public class MainActivity extends AppCompatActivity{
        private Button bindService;
        private Button cancelBindService;
        private MyService.DownloadBinder downloadBinder;
        private ServiceConnection serviceConnection=new ServiceConnection(){
            @Override
            public void onServiceDisconnected(ComponentName name){}
    
            @Override
            public void onServiceConnected(ComponentName name, IBinder service){
                downloadBinder=(MyService.DownloadBinder)service;
                downloadBinder.startDownload();
                downloadBinder.getProgress();
            }
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            bindService=(Button)findViewById(R.id.bindService);
            cancelBindService=(Button)findViewById(R.id.cancelBindService);
    
            bindService.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(MainActivity.this,MyService.class);
                    bindService(intent,serviceConnection,BIND_AUTO_CREATE);
                    startService(intent);
                }
            });
    
            cancelBindService.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    unbindService(serviceConnection);
                    Intent intent=new Intent(MainActivity.this,MyService.class);
                    stopService(intent);
                }
            });
        }
    }
  • 相关阅读:
    C语言函数sscanf()的用法(转)
    GDB基本命令(整合)(转)
    单元命中率与字节命中率
    rpm安装找不到.so库文件(linux动态库连接的相关知识)(转)
    Linux下的tar压缩解压缩命令详解(转)
    Linux系统如何查看版本信息
    Linux /bin, /sbin, /usr/bin, /usr/sbin 区别(转)
    rsync问题-connection refused(111)、Unknown module
    转: 浅析Fusion-IO和Intel SSD
    转: 从0到1的电商架构应该怎么做?
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/5505985.html
Copyright © 2011-2022 走看看