zoukankan      html  css  js  c++  java
  • Service学习

    新建一个用于service的class,Superclass属性设置为Service-android.app, 命名为Pservice在其中复写方法,代码如下:

    @Override
        public void onCreate() {
            // TODO Auto-generated method stub
            Log.i(TAG, "EXampleService--->onCreate");
            super.onCreate();
        }
    
        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            Log.i(TAG, "EXampleService--->onDestroy");
            super.onDestroy();
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // TODO Auto-generated method stub
            Log.i(TAG, "EXampleService--->onStartCommand");
            return super.onStartCommand(intent, flags, startId);
        }
    View Code

    在MainActivity.java中通过write如下监听器来测试service:

        private OnClickListener listener = new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,Pservice.class);
                switch(v.getId()) {
                    case R.id.btnStartService: startService(intent); break;
                    case R.id.btnStopService:  stopService(intent); break;
                    default:                   break;
                }
            }
        };
    View Code

    但需要在Manifest中加入:<service android:name=".Pservice"></service> 这行代码与Activity同级。

    Binder service:

    Binder service:允许其他组件(如Activity)绑定到这个service上,可以发送请求,也可以接受请求,甚至可以进行进程间的通话。Binder service仅仅在服务于其他组件时存在,不能独自无限期的在后台运行。
    View Code

    创建一个servide类在里面复写onBind,代码如下:

    public class BoundService extends Service {
        
        private static final String TAG = "BoundService";
        private MyBinder binder = new MyBinder();
        
        private class MyBinder extends Binder {
            public BoundService getService() {
                return BoundService.this;
            }
        }
        @Override
        public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return binder;
        }
        
        public void MyMethod() {
            Log.i(TAG, "MyMethod()");
        }
    }
    View Code

    创建一个BinderActivity扩展自Activity,代码如下:

    package com.example.pmusicplayer;
    
    import android.os.Bundle;
    import android.os.IBinder;
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class BinderActivity extends Activity {
        
        private Button btnStartBinderService;
        private Button btnStopBinderService;
        private boolean isConnected = false;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.binder);
            
            btnStartBinderService = (Button)findViewById(R.id.btnStartBinderService);
            btnStopBinderService = (Button)findViewById(R.id.btnStopBinderService);
            btnStartBinderService.setOnClickListener(listener);
            btnStopBinderService.setOnClickListener(listener);
        }
        
        private OnClickListener listener = new OnClickListener() {
            public void onClick(View v) {
                switch(v.getId()) {
                    case R.id.btnStartBinderService: bindService(); break;
                    case R.id.btnStopBinderService:  unBind(); break;
                    default:                   break;
                }
            }
        };
        
        private void unBind() {
            if(isConnected) {
                unbindService(conn);
            }
        }
        
        private void bindService() {
            Intent intent = new Intent(BinderActivity.this, BoundService.class);
            bindService(intent, conn, Context.BIND_AUTO_CREATE);
        }
        
        private ServiceConnection conn = new ServiceConnection() {
            @Override
            public void onServiceDisconnected(ComponentName name) {
                // TODO Auto-generated method stub
                isConnected = false;
            }
            
            @Override
            public void onServiceConnected(ComponentName name, IBinder binder) {
                // TODO Auto-generated method stub
                BoundService service=null;
                service.MyMethod();
                isConnected = true;
            }
        };
        
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.binder, menu);
            return true;
        }
    
    }
    View Code
  • 相关阅读:
    mybatis date类型比较
    搭建 c 语言环境 1_centos6 minimal 配置 c/c++ 编译环境
    2_eclipse配置c/c++环境
    1_eclipse导入hibernate 的DTD 文件
    1_eclipse配置c/c++开发环境
    2_修改Eclipse里面的快捷键
    1_修改注释内容
    8_对象创建、static 关键字、静态变量和成员变量的区别、文档
    7_匿名对象、封装(private)、this 关键字、构造方法
    6_面向对象基础、成员变量和局部变量的区别
  • 原文地址:https://www.cnblogs.com/zhang1107/p/3126803.html
Copyright © 2011-2022 走看看