Service(一)----->简单计算
来源:互联网 发布:linux vi 怎么查找 编辑:程序博客网 时间:2024/06/06 00:21
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>
0 0
- Service(一)----->简单计算
- Android Bound Service(一) ----- Extending Binder Service(进程内绑定Service的简单例子)
- Android 简单应用-身高体重指数(BMI)计算一
- 【Matlab学习笔记】(一)初识Matlab和简单计算
- 写给外行的计算机原理(一)简单的计算
- HelloWord! (一) 搭建简单的REST Service -- Service
- 解析service(一)
- APIDemo Service(一)
- 初识 Service(一)
- service (一)用法
- Windows service(一)
- Service详解(一)
- android Service(一)
- Feature Service(一)
- 聊聊Service(一)
- service小结(一)
- Service(一)简介
- Andriod Service(一)
- poj--1459--Power Network(最大流,超级源超级汇)
- 从TimePicker中获取NumberPicker,从NumberPicker获取mSelectionDivider
- Hive-命令行基本操作和java API访问hive数据库
- deep learning 模型简介之CNN卷积网络(一)深度解析CNN
- ubuntu下安装pycharm
- Service(一)----->简单计算
- 树链剖分学习笔记
- 网络原理之OSPF协议中RID、DR/BDR的选举规则
- java贪吃蛇小游戏
- javaBean的介绍及其简单应用
- asp.net 验证码
- 利用highcharts完成折线图。(纯java代码拼接js代码形成基础类,直接用方法即可)
- IntentService 串联 按顺序执行(此次任务执行完才执行下一个任务)
- 多播与广播详解(二)