Service,AIDL

服务的运行不依赖于任何用户界面,即使程序被切换到后台,或者用户打开另一个应用,服务仍然能保持独立正常运行。

服务依赖于创建服务时所在的应用程序进程,当此应用程序进程被杀死,所依赖于该进程的服务也会停止。

服务并不会自动开启线程,所有的代码都是默认运行在主线程中。
需要在服务内部手动创建子线程,执行具体任务,否则容易造成线程阻塞。
服务的基本用法
Context提供API
startService(Intent intent);  开启服务
stopService(Intent intent);  暂停服务

1.自定义服务
public class MyService extends Service {

    private static final String TAG = "MyService";
    int i = 0;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        Log.i(TAG, String.valueOf(i++));
                    }
                }, 1000, 2000);
            }
        }).start();

        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}


2.mainfests中声明服务
<application>
    <service android:name=".service.MyService"/>
</application>


3.在Activity中开启服务
public class MainActivity extends AppCompatActivity {

    private Button mBtnSitchService;
    private Context mContext;
    private boolean switchFlag;
    private Intent mServiceIntent;
  

    private void initEvent() {
        mBtnSitchService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (switchFlag) {
                    stopService();
                } else {
                    startService();
                }
                switchFlag = !switchFlag;
            }
        });
    }

    private void startService() {
        //开启服务
        mServiceIntent = new Intent(this, MyService.class);
        startService(mServiceIntent);
    }

    private void stopService() {
        //暂停服务
        stopService(mServiceIntent);
    }
}
IntentService
因为服务所有代码默认还是运行在主线程,每次做耗时操作都要开启线程,否则会出现ANR,太过繁琐。

Android提供了线程服务,直接运行在子线程中,并且每次运行完会自动关闭服务。


public class MyService extends IntentService {

    private static final String TAG = "MyService";

    public MyService() {
        super("MyIntentService");//调用父类有参的构造函数
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.i(TAG, "Thread id is" + Thread.currentThread().getId());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
服务的生命周期
每个服务都只会存在一个实例。
不管调用多少次startService(),只需要调用一次stopService()或stopSelf(),服务就会停下来。

public class MyService extends Service {

    private static final String TAG = "MyService";

    @Override
    public void onCreate() {
        super.onCreate();
        log("服务第一次创建时调用");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        log("服务停止或解绑时调用");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        log("服务创建后,每次启动连接时调用");
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        log("服务绑定时调用");
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        log("服务解绑时调用");
        return super.onUnbind(intent);
    }

    private void log(String msg) {
        Log.i(TAG, msg);
    }
}
两种服务启动方式
1.Intent service = new Intent(this, MyService.class);
startService(service);
stopService(service);

启动时Service调用的API
onCreate()
onStartCommand(Intent intent, int flags, int startId)

停止时调用的API
onDestroy() 
2.Intent service = new Intent(this, MyService.class);
bindService(service, conn, BIND_AUTO_CREATE)
unbindService(conn);

绑定时调用Service调用的API
onCreate()
onBind(Intent intent)

解绑时调用的API
onUnbind(Intent intent)
onDestroy()
两种启动的区别
startService()
服务启动后,会在后台一直运行,退出程序不会被销毁,但是不能交互数据。

bindService()
能够交互数据,但是,当启动的组件(Activity)被销毁时,Service也会跟着销毁。
注意:一定要再onDestroy()里面调用unbindService(conn)解绑服务。
混合启动
在开发应用时一般两种服务混合启动
先start再Bind,同一个绑定服务不能多次解绑,否则会报错。

private void startService() {
    mServiceIntent = new Intent(this, MyService.class);

    //开启服务
    startService(mServiceIntent);
    bindService(mServiceIntent, mConn, Service.BIND_AUTO_CREATE);
}

private void stopService() {
    //暂停服务
    unbindService(mConn);
    stopService(mServiceIntent);
}

混合启动解绑
需要先调用stopService(),再调用unbindService(),onDestroy()才会执行。

Service通讯
注意:
任何一个服务在整个应用程序范围内都是通用的,可以和任何一个活动绑定。
绑定后,可以获取相同的Binder实例。

定义接口,用来构建Service和Activity的通信桥梁
public interface IMyBindDemo {
    void call();
}


自定义服务
public class MyService extends Service {

    private static final String TAG = "MyService";

    class MyBind extends Binder implements IMyBindDemo {

        @Override
        public void call() {
            Log.i(TAG, "Service Demo");
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        //绑定服务时,绑定接口
        return new MyBind();
    }
}


开启服务,获取服务内的数据
public class MainActivity extends AppCompatActivity {

    private Button mBtnSwitchService,mBtnGetServiceData;
    private Context mContext;
    private boolean switchFlag;
    private Intent mServiceIntent;
    private IMyBindDemo mBind;

    //服务连接
    private ServiceConnection mConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName,
                                       IBinder iBinder) {
            
            //iBinder 与 onBind的返回值绑定
            mBind = (IMyBindDemo) iBinder;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
    }

    private void initEvent() {
        mBtnSwitchService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (switchFlag) {
                    stopService();
                } else {
                    startService();
                }
                switchFlag = !switchFlag;
            }
        });

        mBtnGetServiceData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取通信
                mBind.call();
            }
        });
    }

    private void initView() {
        mContext = this;
        mBtnSwitchService = findViewById(R.id.btn_switch);
        mBtnGetServiceData = findViewById(R.id.btn_get_service_data);
    }

    private void startService() {
        mServiceIntent = new Intent(this, MyService.class);

        //开启服务
        startService(mServiceIntent);
        bindService(mServiceIntent, mConn, Service.BIND_AUTO_CREATE);
    }

    private void stopService() {
        //暂停服务
        unbindService(mConn);
        stopService(mServiceIntent);
    }
}
AIDL(Android interface definition Language)

AIDL即Android接口定义语言,是Android提供的一种用于进程间通信 (IPC)的机制。用来定义不同进程间,进行相互通信的接口。

AIDL支持的数据类型
1.  Java 的基本数据类型
2.  List 和 Map
       元素必须是 AIDL 支持的数据类型
       Server 端具体的类里则必须是 ArrayList 或者 HashMap

3.  其他 AIDL 生成的接口
4.  实现 Parcelable 的实体
AIDL的基本实现
举个例子:客户端向服务端发送验证支付请求

服务端
1.  自定义服务接口
参数最好封装成 Parcelable 的实体,为了演示就不封装了。
public interface IAlipayBinder {
    /**
     * @param account 账号
     * @param pwd 密码
     * @param payPwd 支付密码
     * @param money 支付数额
     * @param currTime 当前时间戳
     */
    int callDealAlipay(String account, String pwd, 
                       String payPwd, double money, long currTime);
}
2.  在工程的 main下新建 aidl 文件夹。
    右键新建aidl接口文件,注意包名要和java内的一致。

    新建完成后修改aidl文件内的方法,和定义的接口方法保持一致。
    此时需要重新编译一下工程,否则找不到aidl的接口。
3.1  自定义服务

public class AlipayService extends Service {

    private static final String TAG = "AlipayService";

    //这里不需要修饰符
    class MyBinder extends IAlipayBinder.Stub {
        @Override
        public int callDealAlipay(String account, String pwd,
                                  String payPwd, double money,
                                  long currTime) throws RemoteException {

            return dealAlipay(account, pwd, payPwd, money, currTime);
        }
    }

    @Override
    public void onCreate() {
        Log.i(TAG, "service create: OK");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }


    public int dealAlipay(String account, String pwd,
                          String payPwd, double money,
                          long currTime) {

        if (!account.equals("admin") || !pwd.equals("123")) {
            return 0;
        }

        if (!payPwd.equals("123456")) {
            return 1;
        }

        if (money > 1000) {
            return 2;
        }
        return 3;
    }
}


3.2  在mainfests中声明,注意要加入action,客户端启动时用得到。
<application>
    <service android:name=".service.AlipayService">
        <intent-filter>
            <action android:name="com.w.review.service.ALIPAY" />
        </intent-filter>
    </service>
</application>


3.3  启动服务
private void startService() {
        Intent intent = new Intent(this, AlipayService.class);
        startService(intent);
}
1.  客户端进程
同样新建一个aidl文件夹,将服务端的aidl文件复制过来。
注意保持包名和服务端的包名一致。
2.使用bindService()进行通讯
public class MainActivity extends AppCompatActivity {

    private Button mBtnStart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
    }

    private void initEvent() {
        mBtnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startAlipay();
            }
        });
    }

    private void initView() {
        mBtnStart = findViewById(R.id.btn_start);
    }

    private void startAlipay() {
        Intent intent = new Intent();
        intent.setAction("com.w.review.service.ALIPAY");

        注意,Android5.0后,要想隐式启动Service,需要加包名
        intent.setPackage("com.w.review");

        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName,
                                           IBinder iBinder) {

                IAlipayBinder binder = IAlipayBinder.Stub.asInterface(iBinder);

                try {
                    int state = binder.callDealAlipay(
                            "admin",
                            "123",
                            "123456",
                            100,
                            100L);

                    switch (state) {
                        case 0:
                            showToast("账户或密码错误");
                            break;

                        case 1:
                            showToast("支付密码错误");
                            break;

                        case 2:
                            showToast("余额不足");
                            break;

                        case 3:
                            showToast("支付成功");
                            break;
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {

            }
        }, Service.BIND_AUTO_CREATE);
    }

    private void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
result
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,045评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,114评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,120评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,902评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,828评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,132评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,590评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,258评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,408评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,335评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,385评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,068评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,660评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,747评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,967评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,406评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,970评论 2 341

推荐阅读更多精彩内容

  • 上篇我们讲解了Android中的5中等级的进程,分别是:前台进程、可见进程、服务进程、后台进程、空进程。系统会按照...
    徐爱卿阅读 3,834评论 6 33
  • 转载注明出处:http://www.jianshu.com/p/a1d3d9693e91 1. 简介 与前一篇An...
    王三的猫阿德阅读 1,897评论 1 9
  • Service是一种可以在后台执行耗时操作而没有用户界面的应用组件。它默认运行在主线程中,不可以直接进行耗时操作,...
    stevewang阅读 726评论 0 2
  • 绑定服务: 绑定服务是客户端-服务器接口中的服务器。绑定服务可让组件(例如 Activity)绑定到服务、发送请求...
    pifoo阅读 1,219评论 0 4
  • 跟着洪水的脚印 我找到那势将沉溺的楼宇, 在半没的危梯之下 水玄墨而深谧,如黑夜之不测 在隧洞透明的剖面上 我看见...
    李野航阅读 189评论 0 1