service本地服务
来源:互联网 发布:淘宝客机器人有哪些 编辑:程序博客网 时间:2024/05/24 22:42
1.创建一个servier.java 方法私有化,保存数据
package com.example.service;import com.example.iserveice.IStudent;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class StudentService extends Service { // 根据学号查学生姓名 private String[] names = { "Tom", "John", "Namei", "Mary", "Kitty" }; private String query(int no) { if (no > 0 && no <= names.length) { return names[no - 1]; } return null; } IBinder binder = new MyIbinder(); @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return binder; } public class MyIbinder extends Binder implements IStudent { @Override public String queryStudent(int no) { return query(no); } }}
2.Service属于四大组件,不能够直接new,所以activity想要调用里面的方法,需要先绑定,然后获取里面的私有方法。定义一个接口,让service去实现
package com.example.iserveice;public interface IStudent { public String queryStudent(int no);}
3.页面的展示
package com.example;import android.os.Bundle;import android.os.IBinder;import com.example.iserveice.IStudent;import com.example.service.StudentService;import android.app.Activity;import android.app.Service;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.view.Menu;import android.view.View;import android.widget.EditText;public class MainActivity extends Activity { EditText edit_no; EditText text_name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 edit_no = (EditText) findViewById(R.id.editText1); text_name = (EditText) findViewById(R.id.editText2); Intent service = new Intent(this,StudentService.class); //以绑定方式来启动服务 bindService(service, conn, Service.BIND_AUTO_CREATE); } MyConnection conn = new MyConnection(); IStudent istudent = null; /** * 实现链接的方法 * @author tarena * */ public class MyConnection implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { //当客户端和服务端绑定后回调服务中的OnBind方法传回Binder对象 istudent = (IStudent) service; } @Override public void onServiceDisconnected(ComponentName name) { //解绑服务的时候回调OnBinder的方法 istudent = null; } } //点击事件 public void query(View view){ //获得学号 String no = edit_no.getText().toString(); //根据绑定返回的Service调用相应的方法查询学生的姓名 String name = istudent.queryStudent(Integer.parseInt(no)); text_name.setText(name); } @Override public 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; } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); //解绑服务 unbindService(conn); }}
即可实现在界面输入存在的学号获得相应的姓名
0 0
- 服务(service)-----本地服务
- Service绑定本地服务
- Service之《本地服务》
- service本地服务
- Android Service(本地服务)
- Android Service之本地服务
- Service(一、本地服务)
- Android Service 后台服务之本地服务
- Service(服务)之 Local Service(本地服务)
- Android Service学习之本地服务
- Android Service学习之本地服务
- Android Service学习之本地服务
- Android Service学习之本地服务
- Android Service学习之本地服务
- Android Service学习之本地服务
- 【Android】Service学习之本地服务
- Android Service学习之本地服务
- Android Service学习之本地服务
- HDU ACM 11 2029 Palindromes _easy version
- 尺度不变特征变换匹配算法详解
- Android小知识
- 新的开始
- 3. Longest Substring Without Repeating Characters
- service本地服务
- 基于Dragonboard410c android系统实现USB camera图像预览
- plsql数据库密码过期的解决方法
- Android提高之MediaPlayer播放网络音频的实现方法
- 【指导】virtual box ubuntu 网络配置
- 基于ARM的智能灯光控制系统(2)需求分析
- 为什么学习React Native三点原因
- 启动weblogic服务时,报此时不应有“1.6(1.7)jdk”解决方法
- SpringBoot配置文件以及配置项