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>

Service(一)----->简单计算的更多相关文章

  1. SDUT OJ 2616 简单计算

    简单计算 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 某天,XX 给YY 出了一道题,题目是: 给出n 个十进制的数,找出这n ...

  2. Python简单计算数组元素平均值的方法示例

    Python简单计算数组元素平均值的方法示例 本文实例讲述了Python简单计算数组元素平均值的方法.分享给大家供大家参考,具体如下: Python 环境:Python 2.7.12 x64 IDE ...

  3. csps模拟9495凉宫春日的忧郁,漫无止境的八月,简单计算,格式化,真相题解

    题面:https://www.cnblogs.com/Juve/articles/11767239.html 94,95的T3都没改出来,是我太菜了... 凉宫春日的忧郁: 比较$x^y$和$y!$的 ...

  4. C# 服务端篇之实现RestFul Service开发(简单实用)

    一.RestFul简介 REST(Representational State Transfer 通常被翻译为“表述性状态传输”或者“表述性状态转移”)是RoyFielding提出的一个描述互联系统架 ...

  5. Visual Studio 2013中引入Web Service的简单方法visual studio 引用 wsdl

    http://blog.csdn.net/wangzhongbo_24/article/details/49954191 Web Service有三种表示方式 三种方式分别为WSDL.Endpoint ...

  6. Android Service使用简单介绍

    作为一个android初学者,经常对service的使用感到困惑.今天结合Google API 对Service这四大组件之一,进行简单使用说明. 希望对和我一样的初学者有帮助,如有不对的地方,也希望 ...

  7. SQL Server Service Broker 简单例子 (转)

    SQL Server Service Broker服务体系结构 消息类型 — 定义应用程序间交换的消息的名称.还可以选择是否验证消息.约定 — 指定给定会话中的消息方向和消息类型.队列 — 存储消息. ...

  8. Scala简单计算实例,其在数据分析方面的优势体会

    程序只是简单的从文件中读取数据,并进行计算. package com.bill.www /** * Created by Bill on 2016/2/3. * 目的:用scala实现简单的数据计算 ...

  9. service的简单使用

    Service的生命周期方法比Activity少一些,只有onCreate, onStart, onDestroy 我们有两种方式启动一个Service,他们对Service生命周期的影响是不一样的. ...

随机推荐

  1. (easy)LeetCode 217.Contains Duplicate

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  2. 9. Palindrome Number

    /* Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers ...

  3. iBeacons 资源汇总

    从淘宝上购买了iBeacons的开发模块,便开始要熟悉这个技术了. 先来说一下什么是iBeacons iBeacons是苹果在2013年WWDC上推出一项基于蓝牙4.0(Bluetooth LE | ...

  4. VC ClistCtrl不同行背景色

    大家经常用到listctrl 做表格 为了好看,要给每行设置不同背景色.很实用的一个小技巧. 1:首先在.h里添加以下消息 afx_msg void OnDrawColorForMyList(NMHD ...

  5. jmeter随笔(2)--上传接口报错

    黑夜小怪(2016-8-24  23:45) 微信订阅号: 问题:今天同事遇到问题,一个图片上传接口,单独跑是ok的,但是放在和其他接口一起就跑不通,如图 分析:查看该接口fiddler的抓包,发现请 ...

  6. POJ1979 Red and Black (简单DFS)

    POJ1979 Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  7. 8051学习笔记——AD

    AD.C #include<reg52.h> #include <iic.h> #define PCF8591 0x90 //PCF8591 地址 sbit LS138A=P2 ...

  8. oracle中的针对该库的表

    ALL_TAB_COLUMNS:所有用户的表字段 USER_TAB_COMMENTS:当前用户的所有表备注 USER_COL_COMMENTS:当前用户的所有列备注 USER_TAB_COLUMNS: ...

  9. 理解CSS中BFC

    BFC(Block Formatting Context) 是Web页面中盒模型布局的CSS渲染模式.它的定位体系 属于 常规文档流 .摘自 W3C : 浮动,绝对定位元素, inline-block ...

  10. PayPal 开发详解(六):下载paypal立即付款SDK 并编译打包

    1.下载PayPal REST SDKs,地址:https://developer.paypal.com/docs/api/rest-sdks/ paypal api比较混乱,有的已经不推荐使用,比如 ...