与服务通信

用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(); }
}

最新文章

  1. 【linux草鞋应用编程系列】_4_ 应用程序多线程
  2. 手动获取spring的ApplicationContext和bean对象
  3. &&和||的妙用
  4. C#出题库项目的总结(1)
  5. partition实现
  6. -webkit-appearance: none;去处select默认小箭头样式
  7. __attribute__机制介绍
  8. Hive学习之四 《Hive分区表场景案例应用案例,企业日志加载》 详解
  9. C语言数组的学习
  10. 基于.NET的微信SDK
  11. mac下安装 resin 奇葩问题总结
  12. 锋利的jQuery(1)——DOM对象与jQuery对象的转换
  13. Django创建应用、模型、配置后台自动管理
  14. [JLOI2015]城池攻占
  15. 一起来搭简单的App框架
  16. 非常易于理解‘类'与'对象’ 间 属性 引用关系,暨《Python 中的引用和类属性的初步理解》读后感
  17. 前端安全类——CSRF/XSS
  18. pycahrm 安装Vue项目
  19. 面试总结——JVM篇
  20. fork()、vfork()、clone()的区别

热门文章

  1. 初识mongo
  2. MJExtension的使用
  3. MVC4相关Razor语法以及Form表单(转载)
  4. connectVisualVMtoTomcat
  5. english 释词
  6. Oulipo HDU 1686 KMP模板
  7. linux的学习系列 8---进程管理
  8. webview h5页面 关闭
  9. myeclipse中常用的快捷键
  10. ant脚本