美文网首页
Android学习小记-Service

Android学习小记-Service

作者: GaoEnron | 来源:发表于2019-06-29 21:51 被阅读0次

    Service:实现程序在应用后台运行

    1. 创建Service文件
    // new Intent(MainActivity.this, MyService.class)创建当前和目标的Activity
     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            findViewById(R.id.btnStartService).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startService(new Intent(MainActivity.this, MyService.class));
                }
            });
    
            findViewById(R.id.btnCloseService).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    stopService(new Intent(MainActivity.this, MyService.class));
                }
            });
        }
    
    1. 在Service中重写 onStartCommand 方法
     // 在onStartCommand
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    System.out.println("服务正在运行");
                }
            }.start();
    
    
            return super.onStartCommand(intent, flags, startId);
        }
    

    通过绑定的方式实现启动Service

    // ServiceConnection 实现当前的接口
    public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {
        Intent intent = nil;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            intent = new Intent(MainActivity.this, MyService.class);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    //        findViewById(R.id.btnStartService).setOnClickListener(new View.OnClickListener() {
    //            @Override
    //            public void onClick(View v) {
    //                startService(new Intent(MainActivity.this, MyService.class));
    //            }
    //        });
    //
    //        findViewById(R.id.btnCloseService).setOnClickListener(new View.OnClickListener() {
    //            @Override
    //            public void onClick(View v) {
    //                stopService(new Intent(MainActivity.this, MyService.class));
    //            }
    //        });
            findViewById(R.id.btnStartService).setOnClickListener(this);
            findViewById(R.id.btnCloseService).setOnClickListener(this);
            findViewById(R.id.btnBindService).setOnClickListener(this);
            findViewById(R.id.btnUnbindService).setOnClickListener(this);
    
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case  R.id.btnStartService:
                    startService(intent);
                    break;
                case  R.id.btnCloseService:
                    stopService(intent);
                    break;
                case  R.id.btnBindService:
                    bindService(intent,this, Context.BIND_AUTO_CREATE);
                    break;
                case  R.id.btnUnbindService:
                    unbindService(this);
                    break;
            }
        }
    
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // 服务被绑定时候执行
        }
    
        @Override
        public void onServiceDisconnected(ComponentName name) {
            // 服务崩溃的时候执行
        }
    }
    
    在MyService中 重写
      public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            return  new Binder(); //
        }
    
    
    public class MyService extends Service {
        public MyService() {
    
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            return  new Binder(); //
        }
    
    
        // 在onStartCommand
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    System.out.println("服务正在运行");
                }
            }.start();
    
    
            return super.onStartCommand(intent, flags, startId);
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Android学习小记-Service

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