Android开发入门之建立能与访问者进行相互通信的本地服务
来源:互联网 发布:古玩玉器交友软件 编辑:程序博客网 时间:2024/06/01 07:39
StudentService
package cn.leigo.studentquery;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class StudentService extends Service {private StudentBinder binder = new StudentBinder();private String[] names = { "张三", "李四", "王五" };public String queryStudentByNo(int no) {if (no > 0 && no < 4) {return names[no - 1];}return null;}@Overridepublic IBinder onBind(Intent intent) {return binder;}private class StudentBinder extends Binder implements IStudent {@Overridepublic String queryStudent(int no) {return queryStudentByNo(no);}}}
<service android:name="cn.leigo.studentquery.StudentService" />
IStudent
package cn.leigo.studentquery;public interface IStudent {public String queryStudent(int no);}
MainActivity
package cn.leigo.studentquery;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener {private EditText mStudentNoEditText;private Button mQueryButton;private TextView mResultTextView;private StudentServiceConnection conn = new StudentServiceConnection();private IStudent iStudent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mStudentNoEditText = (EditText) findViewById(R.id.et_student_no);mQueryButton = (Button) findViewById(R.id.btn_query);mResultTextView = (TextView) findViewById(R.id.tv_result);mQueryButton.setOnClickListener(this);Intent service = new Intent(this, StudentService.class);bindService(service, conn, BIND_AUTO_CREATE);}@Overrideprotected void onDestroy() {super.onDestroy();unbindService(conn);}private class StudentServiceConnection implements ServiceConnection {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {iStudent = (IStudent) service;}@Overridepublic void onServiceDisconnected(ComponentName name) {iStudent = null;}}@Overridepublic void onClick(View v) {String no = mStudentNoEditText.getText().toString();String name = iStudent.queryStudent(Integer.parseInt(no));mResultTextView.setText(name);}}
- Android开发入门之建立能与访问者进行相互通信的本地服务
- Android--建立能与访问者进行相互通信的本地服务
- 建立能与访问者相互通信的本地服务
- android学习笔记---40_建立能与访问者进行相互通信的本地服务,后台运行的service
- android学习笔记---40_建立能与访问者进行相互通信的本地服务,后台运行的service
- 建立能和访问者相互通信的本地服务
- Android建立能与Activity进行相互通信的本地服务
- Android:Service:与Activity进行相互通信的本地服务
- Android学习笔记_22_服务Service应用之—与Activity进行相互通信的本地服务
- Project——建立能与服务通信的应用与服务的生命周期
- Android 应用程序建立与WMS服务之间的通信过程
- Android开发入门之服务的生命周期
- Android中js与本地方法进行相互调用
- Android开发之简单的聊天室(客户端与服务器进行通信)
- Android开发之简单的聊天室(客户端与服务器进行通信)
- Android之服务(二)活动和服务进行通信
- Android开发之远程服务[Remote:]通信
- android后台服务service全解析(上)--service的使用与本地通信
- THE COINBASE BLOG
- Can't login Ubuntu graphic desktop after remotely enabling X11vnc (command line OK)
- samba共享使用端口
- UVA 439 (骑士移动 第一道DFS 13.07.17)
- Consistent gets 的理解
- Android开发入门之建立能与访问者进行相互通信的本地服务
- 高效程序员的45个习惯
- Codeforces Round #189 (Div. 1) Kalila and Dimna in the Logging Industry 斜率DP
- CheckedListBox数据绑定及取值技巧
- Java中同步与并发
- CGAffineTransformInvert: singular matrix. 解决方案
- 黑马程序员____基础加强____反射(框架的基石)
- django admin扩展-自定义后台管理界面
- AJAX UTf-8乱码