zoukankan      html  css  js  c++  java
  • Android service的开启和绑定,以及调用service的方法

    界面:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
            >
        <Button android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="开启服务"
                android:onClick="start"
                />
    
        <Button android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="关闭服务"
                android:onClick="close"
                />
    
        <Button android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="绑定服务"
                android:onClick="bind"
                />
    
        <Button android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="取消绑定服务"
                android:onClick="unbind"
                />
    
        <Button android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="调用服务的方法function()"
                android:onClick="callFunction"
                />
    </LinearLayout>

    Activity

    package com.example.serviceTest;
    
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.view.View;
    
    public class MyActivity extends Activity {
    
        private IService myBinder;
        private ServiceConnection myConn = new MyConn();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    
        public void start(View view) {
            startService(new Intent(this, TestService.class));
        }
    
        /**
         * 多次调用停止服务,没有出现问题(不能多次解绑)
         */
        public void close(View view) {
            stopService(new Intent(this, TestService.class));
        }
    
    
        public void bind(View view) {
            Intent intent = new Intent(this, TestService.class);
            bindService(intent, myConn, BIND_AUTO_CREATE);
        }
    
        /**
         * 多次解绑服务会报出错误
         */
        public void unbind(View view) {
            unbindService(myConn);
        }
    
        public void callFunction(View view) {
            if (myBinder != null) {
                myBinder.callFunction();
            }
        }
    
        //绑定的时候,回调的一些方法
        class MyConn implements ServiceConnection {
    
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                System.out.println("获取到binder");
                myBinder = (IService) service;
            }
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
                System.out.println("com.example.serviceTest.MyActivity.MyConn.onServiceDisconnected");
            }
        }
    }


    Service

    package com.example.serviceTest;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    import android.widget.Toast;
    
    /**
     * Created by Heyiyong on 14-5-16.
     */
    public class TestService extends Service {
    
        public IBinder onBind(Intent intent) {
            System.out.println("服务被绑定了!");
            return new MyBinder();
        }
    
        //中间人(service的代理)
        private class MyBinder extends Binder implements IService{
            public void callFunction() {
                function();
            }
        }
    
        @Override
        public void onCreate() {
            System.out.println("com.example.serviceTest.TestService.onCreate");
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            System.out.println("com.example.serviceTest.TestService.onStartCommand");
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public void onDestroy() {
            System.out.println("com.example.serviceTest.TestService.onDestroy");
        }
    
        @Override
        public boolean onUnbind(Intent intent) {
            System.out.println("com.example.serviceTest.TestService.onUnbind");
            return super.onUnbind(intent);
        }
    
        public void function() {
            Toast.makeText(getApplicationContext(), "function()方法被调用了!", 1).show();
            System.out.println("com.example.serviceTest.TestService.function");
        }
    }
  • 相关阅读:
    今天不说技术,说说中国的十二生肖告诉了我们什么?这就是我们的祖先!
    JS函数的原型及对象,对象方法,对象属性的学习
    C#3.0特性之列表对象的赋值更容易
    读本地图像文件,在上面写一些文件,再传到WWW服务器上
    【Visual C++】vs2008/2005正确打开vs2010所创建项目的几种方法
    高级Swing容器(一)
    助你成长为优秀的程序员 杰出的软件工程师、设计师、分析师和架构师
    Root Pane Containers(一)
    【Visual C++】关于无法打开包括文件:“StdAfx.h”或者意外结尾的错误解决方案
    20年工作经验的架构师写给程序员的一封信
  • 原文地址:https://www.cnblogs.com/wuyou/p/3733631.html
Copyright © 2011-2022 走看看