zoukankan      html  css  js  c++  java
  • Android之Service通信-(2)

    一、Service通过IBinder与Activity进行通信

    在Service中进行下载

    Service

    package chuiyuan.lsj.androidjava.service;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    import android.util.Log;
    import android.widget.Toast;
    
    public class DownloadService extends Service {
        private String TAG ="MainService" ;
    
        public class MyBinder extends Binder{
            public DownloadService getService(){
                return DownloadService.this;
            }
        }
    
        public void startDownload() throws InterruptedException{
            //可以看出,这里是在主线程,所以如果真的下载,应该另开一个线程
            Toast.makeText(DownloadService.this,"start download:"+Thread.currentThread().getName(),
                    Toast.LENGTH_LONG).show();
            Thread.sleep(2);
            Toast.makeText(DownloadService.this, "download end", Toast.LENGTH_LONG).show();
        }
    
        //public MyBinder myBinder ;
        public DownloadService() {
        }
    
        @Override
        public void onCreate() {
            Log.d(TAG, "onCreate");
            super.onCreate();
        }
    
        @Override
        public void onDestroy() {
            Log.d(TAG, "onDestroy");
            super.onDestroy();
        }
    
        @Override
        public boolean onUnbind(Intent intent) {
            Log.d(TAG, "onUnbind");
            return super.onUnbind(intent);
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            return  new MyBinder();
        }
    }
    

    测试

    public class DownloadActivity extends BaseActivity{
    
        private DownloadService downloadService;
        private ServiceConnection sc = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                downloadService = ((DownloadService.MyBinder)service).getService();
                try{
                    downloadService.startDownload();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
    
            }
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
    
            }
        } ;
        @Override
        protected void findView() {
            setContentView(R.layout.activity_download);
        }
    
        @Override
        protected void initView() {
    
        }
    
        @Override
        protected void setOnClickListener() {
    
        }
    
        @Override
        public void onClick(View v) {
    
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            Intent bindIntent = new Intent(this, DownloadService.class);
            this.bindService(bindIntent, sc, BIND_AUTO_CREATE);
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            unbindService(sc);
        }
    
    }
    

    二、Service通过Broadcast与Activity通信

    Service 

    /**
     * Service通过BroadCast广播与Activity通信
     * sendBroadcast from Service--->Android System--->
     * Receive by broadcastReceiver in Activity
     */
    public class BroadcastService extends Service {
        private String TAG ="BroadcastService" ;
    
        public class MyBinder extends Binder{
            public BroadcastService getService(){
                return BroadcastService.this;
            }
        } ;
    
        public void sendServiceBroadcast() throws InterruptedException{
            Toast.makeText(BroadcastService.this, "download in thread:"+ Thread.currentThread().getName(),
                    Toast.LENGTH_SHORT).show();
            Intent intent = new Intent() ;
            intent.setAction("chuiyuan.lsj.androidjava.service.broadcastservice") ;
            intent.putExtra("value", 1000) ;
            sendBroadcast(intent);
            Toast.makeText(BroadcastService.this, "send over", Toast.LENGTH_LONG).show();
        }
    
        public BroadcastService() {
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            return  new MyBinder();
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            //如果 是用的startService,可以在这里执行
    //        Toast.makeText(this, "onCreate:send broadcast", Toast.LENGTH_SHORT).show();
    //        try {
    //            sendServiceBroadcast();
    //        }catch (InterruptedException e){
    //            e.printStackTrace();
    //        }
    
        }
    
        @Override
        public void onDestroy() {
            Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();
            super.onDestroy();
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Toast.makeText(this, "onStartCommand", Toast.LENGTH_SHORT).show();
            return super.onStartCommand(intent, flags, startId);
        }
    }
    

    测试

    public class ServiceSendbroadcastActivity extends BaseActivity{
    
        private BroadcastService broadcastService;
    
        private ServiceConnection sc = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                broadcastService = ((BroadcastService.MyBinder)service).getService();
                try{
                    broadcastService.sendServiceBroadcast();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
    
            }
        } ;
    
        @Override
        protected void findView() {
            setContentView(R.layout.activity_service_sendbroadccast);
        }
    
        @Override
        protected void initView() {
            //广播注册
            IntentFilter filter = new IntentFilter() ;
            filter.addAction("chuiyuan.lsj.androidjava.service.broadcastservice");
            registerReceiver(serviceReceiver, filter) ;
        }
    
        @Override
        protected void setOnClickListener() {
    
        }
    
        @Override
        public void onClick(View v) {
    
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            unbindService(sc);
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            Intent bindIntent = new Intent(this, BroadcastService.class) ;
            bindService(bindIntent, sc, BIND_AUTO_CREATE);
        }
    
        public BroadcastReceiver serviceReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Bundle extras = intent.getExtras() ;
                if (extras!= null){
                    if (extras.containsKey("value")){
                        //这里可以做下载,发包等
                        Toast.makeText(ServiceSendbroadcastActivity.this,
                                "receive broadcast:"+extras.get("value"), Toast.LENGTH_SHORT).show();
    
                    }
                }
            }
        } ;
    
    
    }
    

      

      

      

      

  • 相关阅读:
    兴趣遍地都是,专注和持之以恒才是真正稀缺的
    vuecli2和vuecli3项目中添加网页标题图标
    vue+sentry 前端异常日志监控
    从零构建vue项目(三)--vue常用插件
    从零构建vue项目(一)--搭建node环境,拉取项目模板
    dbvisualizer安装
    TS学习笔记----(一)基础类型
    基于weui loading插件封装
    UI组件--element-ui--全部引入和按需引入
    vue_全局注册过滤器
  • 原文地址:https://www.cnblogs.com/chuiyuan/p/4802283.html
Copyright © 2011-2022 走看看