View Code
package com.example.servicedemo; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.AsyncTask; import android.os.Bundle; import android.os.IBinder; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8; private MyService myService; Activity activity = this; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button)findViewById(R.id.btn1); btn2 = (Button)findViewById(R.id.btn2); btn3 = (Button)findViewById(R.id.btn3); btn4 = (Button)findViewById(R.id.btn4); btn5 = (Button)findViewById(R.id.btn5); btn6 = (Button)findViewById(R.id.btn6); btn7 = (Button)findViewById(R.id.btn7); btn8 = (Button)findViewById(R.id.btn8); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); btn5.setOnClickListener(this); btn6.setOnClickListener(this); btn7.setOnClickListener(this); btn8.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName arg0) { // TODO Auto-generated method stub System.out.println("onServiceDisconnected"); myService = null; } @Override public void onServiceConnected(ComponentName name, IBinder binder) { // TODO Auto-generated method stub System.out.println("Service连接成功"); myService = ((MyService.MyBinder)binder).getService(); myService.excute(); System.out.println(((MyService.MyBinder)binder).getBinderName()); } }; @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn1: Intent it1 = new Intent(this, MyService.class); startService(it1); break; case R.id.btn2: Intent it2 = new Intent(this, MyService.class); startService(it2); break; case R.id.btn3: Intent it3 = new Intent(this, MyService.class); startService(it3); break; case R.id.btn4: Intent it4 = new Intent(this, MyService.class); startService(it4); break; case R.id.btn5: //Intent it5 = new Intent(this,MyService.class); //bindService(it5, connection, Context.BIND_AUTO_CREATE); new MyThread().run(); //unbindService(connection); break; case R.id.btn6: Intent it6 = new Intent(this,MyService.class); bindService(it6, connection, Context.BIND_AUTO_CREATE); break; case R.id.btn7: new MyTask().execute(new String[]{"aaa","bbb"}); break; case R.id.btn8: break; default: break; } } //params,progress,result public class MyTask extends AsyncTask<String, String, String>{ @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub System.out.println("doinbackground:"+params.length+ " "+params[0]); for(int i=0;i<10;i++){ publishProgress(params[0]+" "+i); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return "return doinbackground"; } @Override protected void onCancelled() { // TODO Auto-generated method stub System.out.println("onCancelled"); } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub System.out.println("onPostExecute.==>"+result); } @Override protected void onPreExecute() { // TODO Auto-generated method stub System.out.println("onPreExecute"); } @Override protected void onProgressUpdate(String... values) { // TODO Auto-generated method stub System.out.println("onProgressUpdate:"+values.length+" "+values[0]); } } public class MyThread extends Thread{ @Override public void run() { // TODO Auto-generated method stub super.run(); Intent it6 = new Intent(activity,MyService.class); bindService(it6, connection, Context.BIND_AUTO_CREATE); } } }
myservice:
View Code
package com.example.servicedemo; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class MyService extends Service { private final IBinder binder = new MyBinder(); @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub System.out.println("service onBind"); for (int i = 0; i < 10; i++) { try { Thread.sleep(1000); System.out.println("onBind is sleeping."+i); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return binder; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); System.out.println("onCreate service"); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); System.out.println("onStart service"); } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub System.out.println("退出binder"); return super.onUnbind(intent); } public class MyBinder extends Binder{ public String getBinderName(){ return "MyBinder"; } MyService getService(){ return MyService.this; } } public void excute(){ System.out.println("通过binder得到Service来引用service内部方法"); } }