zoukankan      html  css  js  c++  java
  • Activity 调用Service的方法

    一般来说,Activity调用Service 分为两种:进程内调用和进程间调用。进程内调用时比较常用的一种,在进程内调用中我们常常使用的是bindService来启动Service(关于这种启动方式的好处,才疏学浅就不再这卖弄了)。下面就这两种调用方式分别进行简单介绍:

    1.通过bindService来启动Service,并调用Service中的方法。

    1.一个简单的Service:

    package gu.chuan.hang;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    
    public class MyService extends Service {
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return new MyBinder();
        }
    
        public class MyBinder extends Binder{
            public MyService getMyService(){
                return MyService.this;
            }
        }
        /**
         * 在Service中定义这个方法,用于测试
         * @return
         */
        public String getAuthorName(){
            return "guchuanhang";
        }
    }

    2.在Activity中绑定Service并测试效果:

    package gu.chuan.hang;
    
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Intent myServiceIntent = new Intent(MainActivity.this, MyService.class);
            bindService(myServiceIntent, mServiceConnection,
                    Context.BIND_AUTO_CREATE);
        }
    
        ServiceConnection mServiceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                MyService myService = ((MyService.MyBinder) service).getMyService();
                String authorName = myService.getAuthorName();
                Toast.makeText(MainActivity.this, "author name is: " + authorName,
                        Toast.LENGTH_LONG).show();
            }
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
    
        };
    }

    3.截个图吧,更清楚一点: 
    这里写图片描述

    2.进程间启动Service并调用Service中的方法

    1.首先定义一个aidl文件(扩展名为.aidl):

    package gu.chuan.hang.remoteservice.aidl;
    interface AuthorInfo{
     String getAuthorName();
    }

    2.定义一个实现了aidl的服务:

    package gu.chuan.hang.remoteservice.aidl;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.os.RemoteException;
    
    public class MyService extends Service{
    
        @Override
        public IBinder onBind(Intent intent) {
            return new MyStub();
        }
        private class MyStub extends AuthorInfo.Stub{
    
            @Override
            public String getAuthorName() throws RemoteException {
                return "guchuanhang";
            }
    
        }
    
    
    }

    3.在同一个app中模拟进程间调用的效果,给Service设置remote属性:

    <service android:name="gu.chuan.hang.remoteservice.aidl.MyService"
                android:process=":remote"
                android:exported="false" 
                >
    </service>

    4.启动并调用Service中的方法:

    public class MainActivity extends Activity {
           @Override
           public void onCreate(Bundle savedInstanceState){
               super.onCreate(savedInstanceState);
               Intent intent=new Intent(this,MyService.class);
               startService(intent);
               bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
           } 
    
           private ServiceConnection  mServiceConnection=new ServiceConnection(){
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                AuthorInfo authorInfo=AuthorInfo.Stub.asInterface(service);
                try {
                    String authorName=authorInfo.getAuthorName();
                Toast.makeText(MainActivity.this, "author name is: "+authorName, Toast.LENGTH_LONG).show();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
    
    
    
            }
            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
           };
    
    }

    5.附上效果截图: 
    这里写图片描述 
    6.关于aidl进程间调用是参考了http://www.cnblogs.com/zhangdongzi/archive/2012/01/09/2317197.html 
    大神之作,进行的精简修改。

    3.附上Demo下载地址:

    http://download.csdn.net/detail/guchuanhang/9155121

  • 相关阅读:
    定时器的实现
    派遣函数
    IRP的同步
    duilib基本流程
    驱动程序的同步处理
    WFP在包含fwpmu.h头的时候出错
    自己写的驱动用CreateFile打开时错误码返回1的问题
    Windows内核函数
    16_会话技术_Session
    15_会话技术_Cookie
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/6030955.html
Copyright © 2011-2022 走看看