xml:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 tools:context="com.zzw.server.MainActivity" >
6
7 <EditText
8 android:id="@+id/editText1"
9 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 android:layout_alignParentTop="true"
12 android:layout_centerHorizontal="true"
13 android:layout_marginTop="58dp"
14 android:ems="10"
15 android:inputType="number" >
16
17 <requestFocus />
18 </EditText>
19
20 <EditText
21 android:id="@+id/editText2"
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"
24 android:layout_alignLeft="@+id/editText1"
25 android:layout_below="@+id/editText1"
26 android:layout_marginTop="68dp"
27 android:ems="10"
28 android:inputType="number" />
29
30 <Button
31 android:id="@+id/button"
32 android:layout_width="wrap_content"
33 android:layout_height="wrap_content"
34 android:layout_alignParentBottom="true"
35 android:layout_centerHorizontal="true"
36 android:layout_marginBottom="85dp"
37 android:text="相加" />
38
39 </RelativeLayout>
MainActivity:
1 package com.zzw.server;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.view.View;
7 import android.view.View.OnClickListener;
8 import android.widget.EditText;
9
10 public class MainActivity extends Activity {
11 EditText et1, et2;
12
13 @Override
14 protected void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.activity_main);
17 findViewById(R.id.button).setOnClickListener(new OnClickListener() {
18
19 @Override
20 public void onClick(View v) {
21 init();
22 }
23 });
24 }
25
26 public void init() {
27 et1 = (EditText) findViewById(R.id.editText1);
28 et2 = (EditText) findViewById(R.id.editText2);
29 Intent intent = new Intent(MainActivity.this, TestServer.class);
30 int a = Integer.parseInt(et1.getText().toString());
31 int b = Integer.parseInt(et2.getText().toString());
32 int num[] = { a, b };
33 intent.putExtra(Canshu.KEY, num);
34 startService(intent);//开始
35
36 }
37
38 @Override
39 protected void onDestroy() {
40 super.onDestroy();
41 Intent intent = new Intent(MainActivity.this, TestServer.class);
42 stopService(intent);//结束
43 }
44
45 }
Server:
1 package com.zzw.server;
2
3 import android.app.Service;
4 import android.content.Intent;
5 import android.os.IBinder;
6 import android.util.Log;
7 import android.widget.Toast;
8
9 public class TestServer extends Service {
10 //开始只运行一次
11 @Override
12 public void onCreate() {
13 Log.d("=========", "我开始了");
14 super.onCreate();
15 }
16
17 @Override
18 public int onStartCommand(Intent intent, int flags, int startId) {
19 int num[] = intent.getIntArrayExtra(Canshu.KEY);
20 int sum = num[0] + num[1];
21 Toast.makeText(TestServer.this, sum + "", 1).show();
22 return super.onStartCommand(intent, flags, startId);
23 }
24
25 @Override
26 public IBinder onBind(Intent intent) {
27 // TODO Auto-generated method stub
28 return null;
29 }
30
31 @Override
32 public void onDestroy() {
33 Log.d("======", "我被干掉了");
34 super.onDestroy();
35 }
36
37 }
AndroidManifest.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.zzw.server"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk
8 android:minSdkVersion="14"
9 android:targetSdkVersion="21" />
10
11 <application
12 android:allowBackup="true"
13 android:icon="@drawable/ic_launcher"
14 android:label="@string/app_name"
15 android:theme="@style/AppTheme" >
16 <activity
17 android:name=".MainActivity"
18 android:label="@string/app_name" >
19 <intent-filter>
20 <action android:name="android.intent.action.MAIN" />
21
22 <category android:name="android.intent.category.LAUNCHER" />
23 </intent-filter>
24 </activity>
25 <!-- 注册服务器 -->
26 <service android:name="com.zzw.server.TestServer" >
27 </service>
28 </application>
29
30 </manifest>