标签:
我们现在对上一节中的DEMO进行改进,在服务中开启线程来执行。
package com.example.service; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; import android.widget.Button; public class MyService extends Service { public static final String TAG = "MYSERVICE"; private int data; @Override public void onCreate() { Log.i(TAG, "MyService-->onCreate"); new Thread(new Runnable() { @Override public void run() { // 执行具体的任务 getData(); } }).start(); super.onCreate(); } @Override public void onDestroy() { Log.i(TAG, "MyService-->onDestroy"); super.onDestroy(); } private MyBinder binder = new MyBinder(); public class MyBinder extends Binder implements ICalculator { public MyService getService() { return MyService.this; } @Override public int getData() { return data; } } @Override public IBinder onBind(Intent intent) { return binder; } void getData() { try { //模拟耗时的任务 Thread.sleep(10000); data = 100; } catch (InterruptedException e) { e.printStackTrace(); } } }
package com.example.service; public interface ICalculator { int getData(); }
package com.example.servicedemo; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import com.example.service.ICalculator; import com.example.service.MyService; /* * bindService(intent,conn,flag) * Service:onCreate() * Service:onBind() * Activity:onServiceConnected() */ public class MainActivity extends Activity implements OnClickListener { private Button btnStartSrv,btnStopSrv,btnBindSrv; private ICalculator ical; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStartSrv = (Button)findViewById(R.id.btnStartSrv); btnStopSrv = (Button)findViewById(R.id.btnStopSrv); btnBindSrv = (Button)findViewById(R.id.btnBindSrv); btnStartSrv.setOnClickListener(this); btnStopSrv.setOnClickListener(this); btnBindSrv.setOnClickListener(this); Intent intent=new Intent(MainActivity.this,MyService.class); bindService(intent, conn, BIND_AUTO_CREATE); } @Override protected void onDestroy() { unbindService(conn); super.onDestroy(); } @Override public void onClick(View view) { Intent service = new Intent(this, MyService.class); switch(view.getId()) { case R.id.btnStartSrv: { startService(service); break; } case R.id.btnStopSrv: { stopService(service); break; } case R.id.btnBindSrv: { testSrv(); Toast.makeText(this, "服务调用完了", 1).show(); break; } default: break; } } ServiceConnection conn=new ServiceConnection(){ @Override public void onServiceConnected(ComponentName arg0, IBinder service) { ical = (ICalculator) service; } @Override public void onServiceDisconnected(ComponentName arg0) { } }; public void testSrv() { if(ical != null) { int x = ical.getData(); Toast.makeText(this, String.valueOf(x), 1).show(); } } }
淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(五)
标签:
原文地址:http://www.cnblogs.com/yushengbo/p/4613882.html