package com.lixu.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
//定义一个后台类
public class Myservice extends Service { @Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
int[] nums=intent.getIntArrayExtra(Changliang.KEY);
int numhe=nums[0]+nums[1];
Toast.makeText(getApplicationContext(), numhe+"", 1).show();
return super.onStartCommand(intent, flags, startId);
} @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
} }

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity {
EditText et1;
EditText et2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText) findViewById(R.id.et1);
et2=(EditText) findViewById(R.id.et2);
Button button=(Button) findViewById(R.id.btn1);
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Myservice.class);
int[] nums=new int[2];
nums[0]=Integer.parseInt(et1.getText().toString());
nums[1]=Integer.parseInt(et2.getText().toString());
intent.putExtra(Changliang.KEY, nums);
startService(intent);
}
});
} @Override
protected void onDestroy() {
//Activity销毁的时候关闭服务
Intent intent=new Intent(MainActivity.this,Myservice.class);
stopService(intent);
super.onDestroy();
} }

记住在manifest中注册service;

<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=".Myservice" >
</service>
</application>

传递数据后创建后台service来处理事件!的更多相关文章

  1. Itext读取PDF模板文件渲染数据后创建新文件

    Maven导入依赖 <properties> <itextpdf.version>5.5.0</itextpdf.version> <itext-asian. ...

  2. 如何运行后台Service?

    Unless you specify otherwise, most of the operations you do in an app run in the foreground on a spe ...

  3. Java多线程初学者指南(7):向线程传递数据的三种方法

    在传统的同步开发模式下,当我们调用一个函数时,通过这个函数的参数将数据传入,并通过这个函数的返回值来返回最终的计算结果.但在多线程的异步开发模式下,数据的传递和返回和同步开发模式有很大的区别.由于线程 ...

  4. AngularJS + Java---前台网页与后台数据库传递数据 基本结构

    第一个关于这两种语言的项目,以下只是我自己的理解,欢迎指教:) 基本对应关系 1. controller .jsp(.html)  ng-controller="controllerTest ...

  5. Asp.net MVC 传递数据 从前台到后台,包括单个对象,多个对象,集合

    今天为大家分享下 Asp.net MVC 将数据从前台传递到后台的几种方式. 环境:VS2013,MVC5.0框架 1.基本数据类型 我们常见有传递 int, string, bool, double ...

  6. Android开发学习之路-回调实现Service向activity传递数据

    开启服务的时候,如果我们是通过bindService来绑定服务并且要向服务传递数据,可以直接在Intent中设置bundle来达到效果,但是如果是我们需要从服务中返回一些数据到Activity中的时候 ...

  7. MVC中前台如何向后台传递数据------$.get(),$post(),$ajax(),$.getJSON()总结

    一.引言 MVC中view向controller传递数据的时候真心是一个挺让人头疼的一件事情.因为原理不是很懂只看一写Dome,按葫芦画瓢只能理解三分吧. 二.解读Jquery个Ajax函数 $.ge ...

  8. thinkjs学习-this.assign传递数据和ajax调用后台接口

    在页面加载时,就需要显示在页面上的数据,可以在后台使用this.assign赋值,在前台通过ejs等模板获取:用户点击按钮,或者触发某些事件和后台进行交互时,就需要用到ajax调用后台接口.本文通过一 ...

  9. EF5+MVC4系列(7) 后台SelectListItem传值给前台显示Select下拉框;后台Action接收浏览器传值的4种方式; 后台Action向前台View视图传递数据的四种方式(ViewDate,TempDate,ViewBag,Model (实际是ViewDate.Model传值))

    一:后台使用SelectListItem 传值给前台显示Select下拉框 我们先来看数据库的订单表,里面有3条订单,他们的用户id对应了 UserInfo用户表的数据,现在我们要做的是添加一个Ord ...

随机推荐

  1. HDU3085(双向BFS+曼哈顿距离)题解

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  2. ActiveMQ 负载均衡与高可用(转载)

    一.架构和技术介绍 1.简介 ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现 2.activemq的 ...

  3. UVa 120 煎饼

    https://vjudge.net/problem/UVA-120 题意:颠倒连续子序列,使之成为升序. 思路:按照从大到小的顺序,依次选择出一个数字进行分析: ①如果该数字已经在正确的位置上,则不 ...

  4. Loadrunner安装详解

    安装 1. 运行"setup.exe" 2. 点击安装,其中会有提示缺少"Microsoft Visual C++ 2005 SP1运行组件",下载这 个组件. ...

  5. C#完美读取CSV

    /// <summary>         /// 将DataTable中数据写入到CSV文件中         /// </summary>         /// < ...

  6. 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。

    一.需要分析 1.输入为一个字符串和字节数,输出为按字节截取的字符串--->按照字节[byte]截取操作字符串,先将String转换成byte类型 2.汉字不可以截半--->汉字截半的话对 ...

  7. Linux - 命令重定向

    命令重定向, 就是将目前得到的数据转移到指定的地方.分为以下几种: >>>1>2>1>>2>>< 1. > 与 >>先看一 ...

  8. JavaScript权威指南--脚本化HTTP

    知识要点 超文本传输协议(HTTP)规定web浏览器如何从web服务器获取文档和向web服务器发送表单内容,以及web服务器如何响应这些请求和提交.web浏览器会处理大量的HTTP.通常,HTTP并不 ...

  9. 在每个节点填充向右的指针 Populating Next Right Pointers in Each Node

    2018-08-09 16:01:40 一.Populating Next Right Pointers in Each Node 问题描述: 问题求解: 由于是满二叉树,所以很好填充. public ...

  10. 新概念 Lesson 5 How are you today

    How is Emma? 艾玛身体还好吗? 短语:very well How's Emma? She's very well, too. Emma is very well today   adv. ...