Android学习笔记之Service
与服务通信
用bindservice
而startservice并无通信条件.
service 为android为系统服务,所以程序员无法new出来,只能建立服务,共其他组件使用。
package com.jiahemeikang.helloandroid; import com.jiahemikang.service.EchoService;
import com.jiahemikang.service.EchoService.EchoServiceBingder; import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener,ServiceConnection{ private TextView tvResult ;
private Intent serviceIntent;
private Button btnStarAty1;
private Button btnStarService;
private Button btnStopService; private Button btnBingService;
private Button btnUnBingService;
private Button btnGetNum;
public EchoService echoService = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
serviceIntent = new Intent(this,EchoService.class);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = (TextView)findViewById(R.id.tvResult); btnStarAty1= (Button)findViewById(R.id.btnStartAty1);
btnStarService= (Button)findViewById(R.id.btnStartService);
btnStopService= (Button)findViewById(R.id.btnStopService); btnStarService.setOnClickListener(this);
btnStopService.setOnClickListener(this); btnBingService= (Button)findViewById(R.id.bingdingservice);
btnUnBingService= (Button)findViewById(R.id.unbingdingservice);
btnBingService.setOnClickListener(this);
btnUnBingService.setOnClickListener(this); btnGetNum= (Button)findViewById(R.id.btnGetNum);
btnGetNum.setOnClickListener(this); btnStarAty1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){ Intent i = new Intent(MainActivity.this,Aty1.class);
i.putExtra("txt","Hello aty1");
startActivity(i);
startActivityForResult(i,0);
}
}); } @Override
protected void onActivityResult(int a,int b ,Intent i){ if (i!=null) {
String result = i.getStringExtra("result"); tvResult.setText(result);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnStartService:
startService(serviceIntent);
break;
case R.id.btnStopService:
stopService(serviceIntent);
break;
case R.id.bingdingservice:
bindService(serviceIntent,this,Context.BIND_AUTO_CREATE);
break;
case R.id.unbingdingservice:
unbindService(this);
echoService =null;
break;
case R.id.btnGetNum:
if (echoService!=null) {
System.out.print("当前服务的 数字为"+echoService.getCountNum());
}
break;
default:
break;
}
} @Override
public void onServiceConnected(ComponentName arg0, IBinder binder) {
System.out.println("onServiceConnected");
echoService = ((EchoService.EchoServiceBingder)binder).getService(); } @Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub } //onStart }
服务代码
package com.jiahemikang.service; import java.util.Timer;
import java.util.TimerTask; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class EchoService extends Service { public EchoService() { } @Override
public IBinder onBind(Intent arg0) {
System.out.println("onBind");
return echoServiceBingder;
} private Timer timer = null;
private TimerTask task = null; public void startTimer(){
if(timer ==null){ timer = new Timer();
task = new TimerTask(){ @Override
public void run() {
// TODO Auto-generated method stub
i++;
System.out.println("-"+i+"-");
}};
timer.schedule(task,1000,1000);
}
}
private int i =0;
public void StopTimer(){
if (timer!=null) {
timer.cancel();
task.cancel();
timer =null;
task =null;
}
} public int getCountNum(){
return i;
}
private final EchoServiceBingder echoServiceBingder = new EchoServiceBingder();
public class EchoServiceBingder extends Binder{
public EchoService getService(){
return EchoService.this;
}
} @Override
public void onCreate(){
System.out.println("onCreate");
startTimer();
super.onCreate();
} @Override
public void onDestroy(){
System.out.println("onDestroy");
StopTimer();
super.onDestroy(); }
}
Android学习笔记之Service的更多相关文章
- android学习笔记56——Service
Service四大组件之一,需要在AndroidMainfest.xml中添加相关配置,运行于后台,不与用户进行交换,没有UI... 配置时可通过<intent-filter.../>元素 ...
- Android学习笔记--服务(Service)
1.服务概述 1.服务是Android四大组件之一,在使用上可以分为本地服务和远程服务,本地服务是指在不影响用户操作的情况下在后台默默的执行一个耗时操作,例如下载,音频播放等.远程服务是指可以供其他应 ...
- 【转】 Pro Android学习笔记(七七):服务(2):Local Service
目录(?)[-] Local service代码 调用Local ServiceLocal Service client代码 AndroidManifestxml定义Serviceacitivty的l ...
- 【转】 Pro Android学习笔记(七八):服务(3):远程服务:AIDL文件
目录(?)[-] 在AIDL中定义服务接口 根据AIDL文件自动生成接口代码 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.n ...
- 【转】 Pro Android学习笔记(七六):服务(1):local和remote
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ Android提供服务,服务是运行在后台的 ...
- 【转】 Pro Android学习笔记(七四):HTTP服务(8):使用后台线程AsyncTask
目录(?)[-] 5秒超时异常 AsyncTask 实现AsyncTask抽象类 对AsyncTask的调用 在哪里运行 其他重要method 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注 ...
- 【转】 Pro Android学习笔记(七五):HTTP服务(9):DownloadManager
目录(?)[-] 小例子 保存在哪里下载文件信息设置和读取 查看下载状态和取消下载 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件,转载须注明出处:http://blog.csd ...
- 【转】 Pro Android学习笔记(七十):HTTP服务(4):SOAP/JSON/XML、异常
目录(?)[-] SOAP JSON和XMLPullParser Exception处理 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件,转载须注明出处:http://blog. ...
- 【转】 Pro Android学习笔记(六八):HTTP服务(2):HTTP POST
目录(?)[-] 找一个测试网站 HTTP POST小例子 上次学习了HTTP GET请求,这次学习一下HTTP POST. 找一个测试网站 小例子好写,但要找个测试网站就有些麻烦,一下子无从入手,都 ...
随机推荐
- ios做的两个矩形相交叉
#import "ViewController.h" @interface ViewController (){ UIView *_gee; //定义的实例变量 UI ...
- 官方windows10升级工具
https://www.microsoft.com/zh-cn/software-download/windows10
- RS485 介绍
一.RS485总线介绍: RS485总线是一种常见的串行总线标准,采用平衡发送与差分接收的方式,因此具有抑制共模干扰的能力.在一些要求通信距离为几十米到上千米的时候,RS485总线是一种应用最为广泛的 ...
- Ugly Problem
Ugly Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Spec ...
- ReactiveCocoa的冷信号与热信号 探讨
背景 ReactiveCocoa(简称RAC)是最初由GitHub团队开发的一套基于Cocoa的FRP框架.FRP即Functional Reactive Programming(函数式响应式编程), ...
- 【Python初学】深copy&浅copy
在python中,对象赋值实际上是对象的引用.当创建一个对象,然后把它赋给另一个变量的时候,python并没有拷贝这个对象,而只是拷贝了这个对象的引用. 1. copy.copy 浅拷贝 只拷贝父对象 ...
- 在Activity之间使用Intent传值和Bundle传值的区别和方式
两者本质上没有任何区别.Bundle只是一个信息的载体 将内部的内容以键值对组织 Intent负责Activity之间的交互 自己是带有一个Bundle的Intent.putExtras(Bundle ...
- haxe 中使用音效
将wavSound.wav音效文件放到assets/sounds下, 在project.xml加入以下代码: <assets path="assets/sounds" ren ...
- 每个黑客黑客的目标是让目标系统做你不想做的事情。 一个主要的例子是显示敏感文件,如/ etc / passwd和/ etc / shadow(存储用户的用户名和加密密码)。一旦这些文件在他或她的手中,就可以使用“字典“攻击的密码。 或者,他们可以使您的系统FTP受感染的文件,并运行它,这可能是坏或坏。为了做到这一点,他们需要得到一个“可信”的程序来执行他们指定的命令。通常,这是通过“缓冲区
因此,本质上,程序正在读取文本行,解释它们,并基于它们执行操作.这些"网络守护进程"利用的一个方便的特征是它们可以使用"标准输入"和"标准输出&quo ...
- JSP标准标签库(JSTL)--JSTL简介与安装
对于MVC设计模式来讲,我们一直强调,在一个JSP钟scriptlet代码越少越好,但是只靠以前的概念很难实现,因为标签的开发特别麻烦,所以为了简化标签,也为了让标签更具备一些通用性,所以一般在开发中 ...