zoukankan      html  css  js  c++  java
  • 10月9日Android学习笔记:活动与服务之间的通信

    最近在照着《第一行代码》这本书来学安卓,顺便记下笔记。主要的内容是Android中服务的第二种启动方式,通过活动绑定服务来启动服务,实现活动与服务之间的通信。

    一. 首先创建一个服务类

     1 public class MyService extends Service{
     2     
     3     private DownloadBinder mBinder=new DownloadBinder();
     4     
     5     class DownloadBinder extends Binder{
     6         public void startDownload(){
     7             Log.d("MyService", "start download");
     8         }
     9         
    10         public int getProgress(){
    11             Log.d("MyService", "getProgress executed");
    12             return 0;
    13         }
    14     }
    15     
    16     public IBinder onBind(Intent intent){
    17         return mBinder;
    18     }
    19     
    20     public void onCreate(){
    21         super.onCreate();
    22         Log.d("MyService", "onCreate executed");
    23     }
    24     
    25     public int onStartCommand(Intent intent, int flags,int startId){
    26         Log.d("MyService", "onStartCommand executed");
    27         return super.onStartCommand(intent, flags, startId);
    28     }
    29     
    30     public void onDestroy(){
    31         Log.d("MyService", "onDestroy executed");
    32         super.onDestroy();
    33     }
    34 }

    在MyService里重写onCreate(), onStartCommand(), onDestroy()和onBind()方法,这里重点要说下的是onBind()方法,它会在服务被绑定时执行,并且返回一个实现了IBinder接口的实例。这里在MyService中新建了DownloadBinder继承Binder并将其作为onBind()方法的返回值。

    二. 在活动的布局中加入两个按钮用于绑定服务和解除绑定服务

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.example.servicetest.MainActivity" >
        
        <Button 
            android:id="@+id/bind_service"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Bind Service"/>
        
        <Button 
            android:id="@+id/unbind_service"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Unbind Service"/>
    
    </LinearLayout>

    之后修改MainActicity中的代码

    public class MainActivity extends Activity implements OnClickListener{
        
        private Button bindService;
        private Button unbindService;
        private MyService.DownloadBinder downloadBinder;
        private ServiceConnection connection=new ServiceConnection() {
            
            @Override
            public void onServiceDisconnected(ComponentName name) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                // TODO Auto-generated method stub
                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.bind_service);
            unbindService=(Button)findViewById(R.id.unbind_service);
            bindService.setOnClickListener(this);
            unbindService.setOnClickListener(this);
        }
        
        //启动和停止服务
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            switch (arg0.getId()) {case R.id.bind_service:
                Intent bindIntent=new Intent(this,MyService.class);
                bindService(bindIntent, connection, BIND_AUTO_CREATE);
                break;
            case R.id.unbind_service:
                unbindService(connection);
                break;
            default:
                break;
            }
        }
    
    }

    这里的要点:1. ServiceConnection类         2. Activity中的bindService和unbindService方法

    自己的总结:Activity调用自己的bindService方法去绑定服务Service,这个方法里需要3个参数。既然是Activity和Service的绑定,那么这两个类的对象是一定要的,可以将这两个用一个Intent传入。还需要一个对象来代表两者之间的连接,所以第二个参数需要一个ServiceConnection的实例,这个类有两个方法:onServiceConnected和onServiceDisconnected分别在Activity绑定Service和接触绑定时执行。onServiceConnected(ComponentName name, IBinder service)这个方法里的第二个参数应该就是Service在绑定时执行onbind方法时返回的IBinder。

    最后控制台的输出:

  • 相关阅读:
    typescript泛型
    安卓手机IPhone抓包Https
    js里面for循环的++i与i++
    http请求头
    从浏览器地址栏输入url到页面呈现
    docker部署nodejs应用
    mac下使用MongoDB
    使用vue-cli3&vue ui图形化界面创建项目
    javascript中的call, apply(转载)
    跨域请求
  • 原文地址:https://www.cnblogs.com/f91og/p/5943304.html
Copyright © 2011-2022 走看看