package com.improve.service; import java.util.Random; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; /** * 因为Service由主线程执行,耗时操作应该创建独立线程 * @author Davee */ public class AnalysedService extends Service { private static final String TAG = "ServiceAnalysis"; public static final String AUTO_STOP = "auto_stop"; private final Random mRandom = new Random(); @Override public void onCreate() { Log.i(TAG, "onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "onStartCommand flags: " + flags + " startId: " + startId); if (AUTO_STOP.equals(intent.getAction())) { doLastThing(); } /* 返回值告诉Android系统当这个Service被Kill之后(比如当系统内存不足时)后续操作, START_STICKY 表示系统Kill这个Service之后, 如果重新创建这个Service时在调用onStartCommand,不会将最后的Intent作为参数传入 START_REDELIVER_INTENT 则会传入被杀前未处理的最后一个Intent */ return START_STICKY; } @Override public IBinder onBind(Intent intent) { Log.i(TAG, "onBind"); return localBinder; } @Override public boolean onUnbind(Intent intent) { Log.i(TAG, "onUnbind"); return true; } @Override public void onRebind(Intent intent) { Log.i(TAG, "onRebind"); } @Override public void onDestroy() { Log.i(TAG, "onDestroy"); } private final LocalBinder localBinder = new LocalBinder(); public class LocalBinder extends Binder { public AnalysedService getService() { return AnalysedService.this; } } /** * 提供这个方法用个来与客户端(Activity)交互 * @return */ public int getRandomInt() { Log.i(TAG, "getRandomInt"); return mRandom.nextInt(100); } /** * 执行完本方法就停止本服务 */ public void doLastThing() { Log.i(TAG, "doLastThing"); //假设这里下载一个文件,这是个耗时操作 new Thread() { @Override public void run() { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } //文件下载完成后停止本服务 AnalysedService.this.stopSelf();//Service有绑定时不能自动停止,调用无效 } }.start(); } }
package com.improve; import com.improve.service.AnalysedService; import com.improve.service.AnalysedService.LocalBinder; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class ServiceAnalysis extends Activity { private final String TAG = "ServiceAnalysis"; private AnalysedService analysedService; private ServiceConnection sc = new ServiceConnection() { /* 绑定连接建立时调用 */ @Override public void onServiceConnected(ComponentName cn, IBinder binder) { Log.i(TAG, "onServiceConnected"); LocalBinder localBinder = (LocalBinder)binder; analysedService = localBinder.getService(); } /* 在Service意外断开时调用(如Service进程崩溃) 这个例子不会调这个方法,因为它在同一进程中运行 unbindService方法不会调用此方法 */ @Override public void onServiceDisconnected(ComponentName cn) { Log.i(TAG, "onServiceDisconnected"); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.service_analysis); Button start = (Button)findViewById(R.id.service_start); Button stop = (Button)findViewById(R.id.service_stop); Button bind = (Button)findViewById(R.id.service_bind); Button unbind = (Button)findViewById(R.id.service_unbind); Button interact = (Button)findViewById(R.id.service_interact); Button lastThing = (Button)findViewById(R.id.service_last_thing); //每次点击都会调用Service的onStartCommand方法 start.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(ServiceAnalysis.this, AnalysedService.class); ServiceAnalysis.this.startService(intent); } }); //没有开启Service之前调用没意义,但不会有异常 stop.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(ServiceAnalysis.this, AnalysedService.class); ServiceAnalysis.this.stopService(intent); } }); //只会在未绑定时调用Service方法 bind.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { /* 绑定是异步实现的,因此需要通过 ServiceConnection接口来监视与Service之间的连接 */ Intent intent = new Intent(ServiceAnalysis.this, AnalysedService.class); ServiceAnalysis.this.bindService(intent, sc, Context.BIND_AUTO_CREATE); } }); unbind.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //没有bindService之前调用会抛异常,因此调用前需要判断是否绑定 ServiceAnalysis.this.unbindService(sc); } }); interact.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //只要之前调用过Bind(即使又调用了Unbind),就可以调用服务中的方法 Toast.makeText(ServiceAnalysis.this, "从Service取到的整数为 "+analysedService.getRandomInt(), Toast.LENGTH_SHORT).show(); } }); lastThing.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(AnalysedService.AUTO_STOP); intent.setClass(ServiceAnalysis.this, AnalysedService.class); ServiceAnalysis.this.startService(intent); } }); } }