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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android Activity调用 Service,可以使用 Intent 对象来启动 Service。以下是一个简单的示例代码: ```java // 创建 Intent 对象,指定需要启动Service Intent intent = new Intent(this, MyService.class); // 在 Activity启动 Service startService(intent); ``` 在此示例中,我们创建了一个 Intent 对象,指定了要启动Service 类(MyService)。然后,我们调用 `startService()` 方法,将 Intent 对象传递给它,以启动 Service。 如果你希望与 Service 进行通信,可以使用 bindService() 方法。这样,你可以获取 Service 实例,并使用它提供的方法进行交互。 ```java // 创建 Intent 对象,指定需要绑定的 Service Intent intent = new Intent(this, MyService.class); // 在 Activity 中绑定 Service bindService(intent, mConnection, Context.BIND_AUTO_CREATE); // 定义 ServiceConnection 对象,用于获取 Service 实例 private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { // 获取 Service 实例 MyService.MyBinder binder = (MyService.MyBinder) iBinder; MyService service = binder.getService(); // 调用 Service 中的方法 service.doSomething(); } @Override public void onServiceDisconnected(ComponentName componentName) { // 断开连接 } }; ``` 在此示例中,我们创建了一个 Intent 对象,指定了要绑定的 Service 类(MyService)。然后,我们调用 `bindService()` 方法,将 Intent 对象和 ServiceConnection 对象传递给它,以绑定 Service。 在 ServiceConnection 的 `onServiceConnected()` 方法中,我们可以获取 Service 实例,并使用它提供的方法进行交互。在 `onServiceDisconnected()` 方法中,我们可以处理断开连接的情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值