Android笔记三十四.Service综合实例二
- package com.example.aildserver;
- interface Song
- {
- String getName();
- String getSong();
- }
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjYzNzUwMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

- package com.example.aildserver;
- import com.example.aildserver.Song.Stub;
- import android.app.Service;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.IBinder;
- import android.os.RemoteException;
- public class MyService extends Service {
- private String[] names = new String[] {"林俊杰","蔡依林","邓紫棋"};
- private String[] songs = new String[] {"可惜没假设","第三人称","多远都要在一起"};
- private String name,song;
- private int current=1; //当前位置
- private MyBinder binder = new MyBinder(); //实例化一个IBinder对象
- /*0.Stub内部类
- * 该内部类实现了IBinder、Song两个接口,这个Stub类将会作为远程Service的回调类。*/
- public class MyBinder extends Stub
- {
- //a.client回调该方法获取歌手名
- public String getName() throws RemoteException
- {
- return name;
- }
- //b.client回调该方法获取歌曲
- public String getSong() throws RemoteException
- {
- return song;
- }
- }
- /*1.onBind方法
- * service用于返回一个IBinder对象给client方便通信
- */
- @Override
- public IBinder onBind(Intent arg0) {
- return binder;
- }
- /*2.onCreate方法
- * 当Service启动后,自己主动调用该方法,用于初始化
- * */
- public void onCreate() {
- name = names[current]; //给name、song赋值
- song = songs[current];
- System.out.println("Service print:name="+name+"song="+song);
- super.onCreate();
- }
- /*3.onDestroy方法
- * 当訪问者调用Context.stopService方法后。调用该方法关闭Service服务
- * */
- public void onDestroy() {
- super.onDestroy();
- }
- /*4.onUnbind方法
- * 当訪问者调调用Context.unBind()方法后。调用该方法与Service解除绑定*/
- public boolean onUnbind(Intent intent) {
- return false;
- }
- }
但远程Service的onBind()方法仅仅是将IBinder对象的代理传给client的ServiceConnection的onServiceConnected方法的第二个參数。
当client获取了远程的Service的IBinder对象的代理之后,接下来可通过该IBinder对象去回调远程Service的属性或方法。
- <application
- ........
- <!-- 配置service -->
- <service android:name=".MyService">
- <intent-filter>
- <action android:name="com.jiangdongguo.service"/>
- </intent-filter>
- </service>
- </application>
1.拷贝服务端.aidl文件到client

- package com.example.aildclient;
- import com.example.aildserver.Song;
- import android.app.Activity;
- import android.app.Service;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.os.RemoteException;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class MainActivity extends Activity {
- private Button getBtn;
- private EditText song;
- private EditText name;
- private Song binder;
- //1.创建一个ServiceConnection对象
- private ServiceConnection conn = new ServiceConnection()
- {
- public void onServiceConnected(ComponentName name, IBinder service)
- {
- binder = Song.Stub.asInterface(service); //获取Service返回的代理IBinder对象
- }
- public void onServiceDisconnected(ComponentName name) {
- }
- };
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- getBtn=(Button)findViewById(R.id.get);
- song=(EditText)findViewById(R.id.song);
- name=(EditText)findViewById(R.id.name);
- //2.指定要启动的Service
- Intent intent = new Intent("com.jiangdongguo.service");
- bindService(intent, conn, Service.BIND_AUTO_CREATE);
- getBtn.setOnClickListener(new OnClickListener(){
- public void onClick(View arg0)
- {
- try {
- name.setText(binder.getName());
- song.setText(binder.getSong());
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- }
- });
- }
- }

Android笔记三十四.Service综合实例二的更多相关文章
- Android笔记(七十四) 详解Intent
我们最常使用Intent来实现Activity之间的转跳,最近做一个app用到从系统搜索图片的功能,使用到了intent的 setType 方法和 setAction 方法,网上搜索一番,发现实现转跳 ...
- Android笔记(六十四) android中的动画——补间动画(tweened animation)
补间动画就是只需要定义动画开始和结束的位置,动画中间的变化由系统去补齐. 补间动画由一下四种方式: 1.AplhaAnimation——透明度动画效果 2.ScaleAnimation ——缩放动画效 ...
- 【Unity 3D】学习笔记三十五:游戏实例——摄像机切换镜头
摄像机切换镜头 在游戏中常常会切换摄像机来观察某一个游戏对象,能够说.在3D游戏开发中,摄像头的切换是不可或缺的. 这次我们学习总结下摄像机怎么切换镜头. 代码: private var Camera ...
- Android笔记(十四) Android中的基本组件——按钮
Android中的按钮主要包括Button和ImageButton两种,Button继承自TextView,而ImageButton继承自ImageView.Button生成的按钮上显示文字,而Ima ...
- tensorflow学习笔记(三十四):Saver(保存与加载模型)
Savertensorflow 中的 Saver 对象是用于 参数保存和恢复的.如何使用呢? 这里介绍了一些基本的用法. 官网中给出了这么一个例子: v1 = tf.Variable(..., nam ...
- PHP学习笔记三十四【记录日志】
<?php function my_error2($errno,$errmes) { echo "错误号:".$errno; //默认时区是格林威治相差八个时区 //设置 1 ...
- 论文阅读笔记三十四:DSSD: Deconvolutiona lSingle Shot Detector(CVPR2017)
论文源址:https://arxiv.org/abs/1701.06659 开源代码:https://github.com/MTCloudVision/mxnet-dssd 摘要 DSSD主要是向目标 ...
- Python学习日记(三十四) Mysql数据库篇 二
外键(Foreign Key) 如果今天有一张表上面有很多职务的信息 我们可以通过使用外键的方式去将两张表产生关联 这样的好处能够节省空间,比方说你今天的职务名称很长,在一张表中就要重复的去写这个职务 ...
- Java开发学习(三十四)----Maven私服(二)本地仓库访问私服配置与私服资源上传下载
一.本地仓库访问私服配置 我们通过IDEA将开发的模块上传到私服,中间是要经过本地Maven的 本地Maven需要知道私服的访问地址以及私服访问的用户名和密码 私服中的仓库很多,Maven最终要把资源 ...
随机推荐
- 如何发布到NPM上(转)
简要:这篇文章介绍了如何讲自己的包发布到NPM上,马克一下,将来有用 ... npm包发布 发布npm包,更方便以后下载使用. 我们已经把插件代码上传到github上面了,那么我们是否可以也做成一个n ...
- BZOJ 2555 SubString(LCT+后缀树)
喜闻乐见的LCT+SAM 此题要求动态插入,直接上后缀树.然后询问其实就是求一个节点的子树后缀结束节点的个数. 因为建立后缀树需要插入和删除,就直接上LCT.每次加入一个点,把它到根的路径加一 (现在 ...
- HDU 6315 Naive Operations(线段树+复杂度均摊)
发现每次区间加只能加1,最多全局加\(n\)次,这样的话,最后的答案是调和级数为\(nlogn\),我们每当答案加1的时候就单点加,最多加\(nlogn\)次,复杂度可以得当保证. 然后问题就是怎么判 ...
- nginx 多级7层代理安装配置
编译安装 yum install zlib-devel -y wget https://nginx.org/download/nginx-1.15.12.tar.gz tar -zxf nginx-1 ...
- Vue组件使用基础
这篇博文用来记录 .vue 组件的使用方法. 可以把组件代码按照 template.style.script 的拆分方式,放置到对应的 .vue 文件中. 模板(template).初始数据(data ...
- OpenJDK源码研究笔记(四)-编写和组织可复用的工具类和方法
本篇主要讲解java.util.Arrays这个针对数组的工具类. 1.可复用的工具类和方法. 这个工具类里,包含很多针对数组的工具方法,如 排序.交换.二分查找.比较.填充.复制.hashcode ...
- 【BZOJ 1260】[CQOI2007]涂色paint
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 区间DP 设f[i][j]表示i..j这个区间变成目标需要的最少染色次数. f[i][i] = 1 然后考虑f[i][j]的产生方法 ...
- Trustie站点代码托管使用指南
"中国人的github"猛击Trustie官网,開始代码托管... 1. 新建项目 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...
- MPI搭建简要教程
具体安装部署,能够參考 http://www.ibm.com/developerworks/cn/linux/l-cn-mpich2/,该教程将的比較具体. 注:不同版本号的 MPICH2对编译器以及 ...
- ★★★【卡法 常用js库】: js汇合 表单验证 cookie设置 日期格式 电话手机号码 email 整数 小数 金额 检查参数长度
[卡法 常用js库]: js汇合 表单验证 cookie设置 日期格式 电话手机号码 email 整数 小数 金额 检查参数长度 // +---------------------- ...