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. [SQL]一组数据中Name列相同值的最大Je与最小je的差

    declare @t table(name varchar(),qy varchar(),je int) insert into @t union all union all union all un ...

  2. HDU 4081 Qin Shi Huang's National Road System [次小生成树]

    题意: 秦始皇要建路,一共有n个城市,建n-1条路连接. 给了n个城市的坐标和每个城市的人数. 然后建n-2条正常路和n-1条魔法路,最后求A/B的最大值. A代表所建的魔法路的连接的城市的市民的人数 ...

  3. nyoj 61 传纸条

    点击打开链接 传纸条(一) 时间限制:2000 ms  |  内存限制:65535 KB 难度:5 描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做 ...

  4. [HDU 4821] String (字符串哈希)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 题目大意:给你M,L两个字母,问你给定字串里不含M个长度为L的两两相同的子串有多少个? 哈希+枚 ...

  5. (转)ASP.NET MVC4 部署错误 Could not load file or assembly

    使用VS2010 测试ASP.NET MVC 4 Web API 在部署时候遇到了问题,发现园友有解决的方式,因此转载.         我的解决方式有两种:使用VS2015将VS2010的项目重新发 ...

  6. JDK6的switch支持不是很好

    在switch中只支持int或者枚举型值: 不支持其他类型,如String,会报错 Cannot switch on a value of type String for source level b ...

  7. JAVA~多线程:sleep、yield方法

    sleep不考虑其它线程的优先级 yield让位给相同或更高优先级的线程 sleep yield package multiThread2; public class TestThread042Yie ...

  8. Android设置透明、半透明等效果

    设置透明效果 大概有三种 1.用android系统的透明效果Java代码 android:background="@android:color/transparent"  例如 设 ...

  9. Android开发-API指南-<uses-library>

    <uses-library> 英文原文:http://developer.android.com/guide/topics/manifest/uses-library-element.ht ...

  10. struts2拦截器拦截成功后每次请求都出现拦截时的错误信息

    action中验证方法 在执行execute之前执行 @Override    public void validate() {        // TODO Auto-generated metho ...