Service(一)----->简单计算

时间:2022-04-07 14:57:27

xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zzw.server.MainActivity" > <EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="58dp"
android:ems="10"
android:inputType="number" > <requestFocus />
</EditText> <EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="68dp"
android:ems="10"
android:inputType="number" /> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="85dp"
android:text="相加" /> </RelativeLayout>

activity_main.xml

MainActivity:

 package com.zzw.server;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText; public class MainActivity extends Activity {
EditText et1, et2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
init();
}
});
} public void init() {
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
Intent intent = new Intent(MainActivity.this, TestServer.class);
int a = Integer.parseInt(et1.getText().toString());
int b = Integer.parseInt(et2.getText().toString());
int num[] = { a, b };
intent.putExtra(Canshu.KEY, num);
startService(intent);//开始 } @Override
protected void onDestroy() {
super.onDestroy();
Intent intent = new Intent(MainActivity.this, TestServer.class);
stopService(intent);//结束
} }

Server:

 package com.zzw.server;

 import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast; public class TestServer extends Service {
//开始只运行一次
@Override
public void onCreate() {
Log.d("=========", "我开始了");
super.onCreate();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
int num[] = intent.getIntArrayExtra(Canshu.KEY);
int sum = num[0] + num[1];
Toast.makeText(TestServer.this, sum + "", 1).show();
return super.onStartCommand(intent, flags, startId);
} @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
} @Override
public void onDestroy() {
Log.d("======", "我被干掉了");
super.onDestroy();
} }

AndroidManifest.xml:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zzw.server"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 注册服务器 -->
<service android:name="com.zzw.server.TestServer" >
</service>
</application> </manifest>