android应用与服务的通信之学生查询系统案例源码

1.清单文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="cn.caicai.appservice"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk android:minSdkVersion="8" />
 8 
 9     <application
10         android:icon="@drawable/ic_launcher"
11         android:label="@string/app_name" >
12         <activity
13             android:label="@string/app_name"
14             android:name=".Cai_appserviceActivity" >
15             <intent-filter >
16                 <action android:name="android.intent.action.MAIN" />
17 
18                 <category android:name="android.intent.category.LAUNCHER" />
19             </intent-filter>
20         </activity>
21         <!--自定义服务申明 -->
22         <service android:name=".studentservice"></service>
23     </application>
24 
25 </manifest>
AndroidManifest.xml

2.应用界面

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:text="学号:" />
11 
12 
13     <EditText
14         android:id="@+id/studentid"
15         android:layout_width="fill_parent"
16         android:layout_height="wrap_content" />
17 
18     <Button
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21         android:onClick="search"
22         android:text="查询" />
23 
24     <TextView
25         android:id="@+id/result"
26         android:layout_width="wrap_content"
27         android:layout_height="wrap_content" />
28 
29 </LinearLayout>
main.xml

3.服务类文件   //查询的数据可以改为从数据库调用

 1 package cn.caicai.appservice;
 2 
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.os.Binder;
 6 import android.os.IBinder;
 7 
 8 public class studentservice extends Service {
 9     private String[] name = { "小白", "小王", "小软" };
10     private IBinder binder=new studentbinder();
11     public String query(int no) {
12         if (no > 0 && no < 4) {
13             return name[no - 1];
14         }
15         return null;
16     }
17     @Override
18     public IBinder onBind(Intent intent) {
19         return binder;
20     }
21 
22     private class studentbinder extends Binder implements istudent{
23         @Override
24         public String querystudent(int no) {            
25             return query(no);
26         }
27     
28     }
29 }
studentservice.java

4.接口文件

1 package cn.caicai.appservice;
2 
3 public interface istudent {
4  public String querystudent(int no);
5 }
istudent.java

5.应用类文件

 1 package cn.caicai.appservice;
 2 
 3 import android.app.Activity;
 4 import android.content.ComponentName;
 5 import android.content.Intent;
 6 import android.content.ServiceConnection;
 7 import android.os.Bundle;
 8 import android.os.IBinder;
 9 import android.view.View;
10 import android.widget.EditText;
11 import android.widget.TextView;
12 
13 public class Cai_appserviceActivity extends Activity {
14 
15     public TextView result; //结果显示
16     public EditText studentid;//学号
17     private studentserviceconn conn=new studentserviceconn();//自定义服务连接对象
18     private istudent studentbinder; //接口变量
19     @Override
20     public void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.main);
23         result=   (TextView) findViewById(R.id.result);
24         studentid=  (EditText) findViewById(R.id.studentid);
25         Intent service=new Intent(this,studentservice.class);
26         bindService(service, conn, BIND_AUTO_CREATE); //通过意图连接服务,不可用startservice启动
27     }
28     public void search(View v){//点击查询按钮事件
29         String id=studentid.getText().toString();
30         String name=studentbinder.querystudent(Integer.valueOf(id));//通过服务内部对象调用服务方法
31         result.setText(name);
32     }
33     
34     @Override
35     protected void onDestroy() {
36         unbindService(conn);//销毁服务连接
37         super.onDestroy();
38     }
39 
40     private class studentserviceconn implements ServiceConnection{
41 
42         @Override
43         public void onServiceConnected(ComponentName name, IBinder service) {
44             studentbinder=(istudent)service;    //获取服务内部对象        
45         }
46 
47         @Override
48         public void onServiceDisconnected(ComponentName name) {
49             studentbinder=null;        //销毁服务内部对象
50         }
51         
52     }
53 }
Cai_appserviceActivity。java

 

posted on 2013-06-30 13:13  clarenceV1  阅读(893)  评论(0编辑  收藏  举报

导航