访问本地服务里面的方法
来源:互联网 发布:深圳市房地产交易数据 编辑:程序博客网 时间:2024/05/18 11:47
原理图:
步骤:
调用服务里面的方法:
1 设计一个接口:IStudentQueryService Student queryStudent(int no);
2 在activity里面进行绑定操作:
bindService(intent,conn,flag);
3 写一个连接实现类:
private final class MyServiceConnection implements ServiceConnection{
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
ibinder = (IStudnetQueryService) service;
}
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
conn = null;
ibinder = null;
}
}
4 在activity里面用IStudentQueryService 来接收onServiceConnected(ComponentName name, IBinder service)
返回过来的IBinder对象。
5 写一个StudentService extends Service ,重写onBind()
编译一个内部类StudentBinder extends Binder implements IStudnetQueryService
6 创建StudentBinder的对象,通过onBind()方法返回。
7 在activity通过onBind()返回的对象调用服务里面的方法。
代码如下:
1、main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="学号" /> <EditText android:id="@+id/et_no" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="number" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询" android:onClick="query1" /> <TextView android:id="@+id/tv_info" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
2、MainActivity
package com.njupt.studentquery1;import android.os.Bundle;import android.os.IBinder;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.view.Menu;import android.view.View;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {private EditText et_no;private TextView tv_info;private MyServiceConnection conn;private IStudentQueryService ibinder;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main); et_no = (EditText) findViewById(R.id.et_no);tv_info = (TextView) findViewById(R.id.tv_info);conn = new MyServiceConnection();Intent intent = new Intent(this,StudentService.class);bindService(intent,conn,BIND_AUTO_CREATE);}private class MyServiceConnection implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {ibinder = (IStudentQueryService) service;}@Overridepublic void onServiceDisconnected(ComponentName name) {ibinder = null;conn = null;}}@Overrideprotected void onDestroy() {super.onDestroy(); unbindService(conn);}public void query1(View v){String no = et_no.getText().toString();Student student = ibinder.queryStudent(Integer.parseInt(no));tv_info.setText(student.toString());}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
3、StudentService
package com.njupt.studentquery1;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class StudentService extends Service {private StudentBinder ibinder = new StudentBinder();private Student[] students = new Student[]{new Student(1,"章泽天",21),new Student(2,"刘诗诗",22),new Student(3,"allen",21)};@Overridepublic IBinder onBind(Intent intent) {return ibinder;}private class StudentBinder extends Binder implements IStudentQueryService{public Student queryStudent(int no){return query(no);}}public Student query(int no){return students[no - 1];}}
4、IStudentQueryService
package com.njupt.studentquery1;public interface IStudentQueryService {public Student queryStudent(int no);}
5、Student
package com.njupt.studentquery1;public class Student {private int id;private String name;private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Student(int id, String name, int age) {super();this.id = id;this.name = name;this.age = age;}public Student() {super();}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";}}
6、AndroidManifest.xml
记得在清单文件中吧service 给注册上去
<service android:name="com.njupt.studentquery1.StudentService"/>
- 访问本地服务里面的方法
- 采用aidl技术去访问远程服务里面的方法
- 采用aidl访问远程服务里面的方法
- android的httppost方法访问本地web服务(一)
- android的httppost方法访问本地web服务(二)
- 绑定服务&调用服务里面的方法
- 解决“在本地计算机无法启动server服务 错误5:拒绝访问 ”的方法
- 简单搭建nodeJS服务,访问本地站点文件,nodejs改变代码不需要重启的方法
- Android 访问本地服务
- 利用广播调用服务里面的方法
- Android--调用服务里面的方法
- 通过bindService调用服务里面的方法
- 局域网访问虚拟机里面的Web的服务
- 46.android开启服务,调用服务里面的方法
- zookeeper 只能本地访问的问题解决方法
- 解决Codeforces访问慢的本地方法
- 访问本地json文件的方法
- 本地Manager访问远程服务
- 轻松搞定面试中的链表题目
- OS1 绪论
- Smali--Dalvik虚拟机指令语言-->【android_smali语法学习一】
- hdu 4741 Save Labman No.004
- hdu 1023 - Train Problem
- 访问本地服务里面的方法
- eclipse maven plugin 插件 安装 和 配置
- HDU4739
- 调试小技巧
- Eclipse中在线安装Genymotion插件
- HDU 2242(考研路茫茫——空调教室)
- 数字图像处理--几个MATLAB函数
- uva 10361 - Automatic Poetry (简单字符串)
- Rails多国语言