跨应用启动Service并传递数据

启动:

在Android5.0之前可以通过隐式intent启动服务,但是Android5.0之后不可以了。

在两个Application之间是不可能获取到Service的定义的,需要调用SetComponent函数:

        serviceIntent = new Intent();
        serviceIntent.setComponent(new ComponentName("com.wanxiang.www.startservicefromanotherapp", "com.wanxiang.www.startservicefromanotherapp.AppService"));
        serviceIntent.putExtra("data", "Hello AppService");

通过这种方法可以指定要启动的Service的Application,并且也同样可以传递数据过去。但是有个前提,被启动的这个Service所在的Activity必须在运行过程中的。

同样也可以通过绑定服务来启动远程Service

                bindService(new Intent(this, MyService.class), this, BIND_AUTO_CREATE);

本地Activity中的onServiceConnected也会被执行:

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        System.out.println("Bind service");
        System.out.println(iBinder);
}

传递数据:

首先在远程Activity中建一个本地Activity包名的目录,把本地Activity中的AIDL文件拷贝一份过去,可以在onBind()中返回这个接口的stub,并在这个接口中添加函数

@Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return new IAppServiceRemoteBinderInterface.Stub() {
            /**
             * Demonstrates some basic types that you can use as parameters
             * and return values in AIDL.
             *
             * @param anInt
             * @param aLong
             * @param aBoolean
             * @param aFloat
             * @param aDouble
             * @param aString
             */
            @Override
            public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

            }

            @Override
            public void setData(String data) throws RemoteException {
                AppService.this.data = data;
            }
        };
    }

在远程Activity中的onServiceConnected函数中返回这个对象,并保存在类的实例变量中:

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        System.out.println("Bind service");
        System.out.println(iBinder);
        binder = IAppServiceRemoteBinderInterface.Stub.asInterface(iBinder);
    }

    private IAppServiceRemoteBinderInterface binder = null;

然后就可以通过这个变量来调用AIDL中添加的函数,并传递数据给Service。

            case R.id.btnSync:
                if (binder != null)
                    try {
                        binder.setData(etInput.getText().toString());
时间: 2024-08-08 17:33:19

跨应用启动Service并传递数据的相关文章

Android中Service通信(一)——启动Service并传递数据

启动Service并传递数据的小实例(通过外界与服务进行通信): 1.activity_main.xml: <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="默认信息" android:id="@+id/etData"/> <Button android:text=&

Android -- 启动Service并传递数据

本文主要记录Activity传递数据到Service. 1.效果图2.通过以上效果图,可以看出activity页面的数值改变,相应后台service输出的数值也跟着改变.3.核心代码如下,看代码中的38行,使用Intent作为载体,装载activity页面上的数据. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

Android -- 跨应用启动Service

本章博客,记录的是跨应用启动Service. 我们需要创建一个应用service1,其中包含一个MyService,部分代码如下: @Override public void onCreate() { // TODO Auto-generated method stub System.out.println("onCreate"); super.onCreate(); } @Override public void onDestroy() { System.out.println(&q

Android开发之AIDL的使用一--跨应用启动Service

启动其他App的服务,跨进程启动服务. 与启动本应用的Service一样,使用startService(intent)方法 不同的是intent需要携带的内容不同,需要使用intent的setComponent()方法. setComponent()方法需要传入两个参数,第一个参数是包名,第二个参数是组件名.即,第一个参数传入要启动的其他app的包名,第二个参数传入的时候要启动的其他app的service名. 看下面的例子:(aidlserviceapp应用通过button启动aidlservi

【XFeng安卓开发笔记】四大基本组件——跨应用启动service

APP MainActivity.java package com.xfeng.startservicefromanotherapp; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected vo

跨应用启动Service

1 package com.example.metrox.l16; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.IBinder; 6 7 public class AppService extends Service { 8 9 10 public AppService() { 11 } 12 13 @Override 14 public IBinder onBind(I

入门篇:10.Android中AIDL(安卓接口定义语言)跨应用操作Service

Android系统中的进程之间不能共享内存,因此,需要提供一些机制在不同进程之间进行数据通信. 为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用(Remote Procedure Call,RPC)方式来实现.与很多其他的基于RPC的解决方案一样,Android使用一种接口定义语言(Interface Definition Language,IDL)来公开服务的接口.我们知道4个Android应用程序组件中的3个(Activity.BroadcastRec

Android第一行代码学习笔记六---Intent向活动传递数据

@1.向下一个活动传递数据: Intent提供了一系列putExtra()方法的重载,可以把我们想要传递的数据暂存在Intent中,启动了另一个活动后,只需把这些数据再从Intent中取出就可以了,比如firstActivity中有一个字符串要传递到secondActivity中,修改firstActivity中按钮点击事件,代码可以这样编写: button.setOnClickListener(new View.OnClickListener() { public void onClick(V

Android -- 跨应用绑定service(AIDL)

之前一章记录了跨应用启动service,之前学习过startService于bindService的区别及用法. 使用bindService方法,activity需要service返回一个Binder对象,那么如果是两个应用怎么实现呢,这里就涉及到了一个AIDL的概念. AIDL (Android Interface Definition Language) 是一种IDL 语言,用于生成可以在Android设备上两个进程之间进行进程间通信(interprocess communication,