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