Android(安卓)Service分析
16lz
2021-12-04
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();@Overridepublic void onCreate() {Log.i(TAG, "onCreate");}@Overridepublic 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;}@Overridepublic IBinder onBind(Intent intent) {Log.i(TAG, "onBind");return localBinder;}@Overridepublic boolean onUnbind(Intent intent) {Log.i(TAG, "onUnbind");return true;}@Overridepublic void onRebind(Intent intent) {Log.i(TAG, "onRebind");}@Overridepublic 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() {@Overridepublic 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() {/* 绑定连接建立时调用 */ @Overridepublic void onServiceConnected(ComponentName cn, IBinder binder) {Log.i(TAG, "onServiceConnected");LocalBinder localBinder = (LocalBinder)binder;analysedService = localBinder.getService();}/* 在Service意外断开时调用(如Service进程崩溃) 这个例子不会调这个方法,因为它在同一进程中运行 unbindService方法不会调用此方法 */@Overridepublic void onServiceDisconnected(ComponentName cn) {Log.i(TAG, "onServiceDisconnected");}};@Overridepublic 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() {@Overridepublic void onClick(View arg0) {Intent intent = new Intent(ServiceAnalysis.this, AnalysedService.class);ServiceAnalysis.this.startService(intent);}});//没有开启Service之前调用没意义,但不会有异常stop.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Intent intent = new Intent(ServiceAnalysis.this, AnalysedService.class);ServiceAnalysis.this.stopService(intent);}});//只会在未绑定时调用Service方法bind.setOnClickListener(new OnClickListener() {@Overridepublic 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() {@Overridepublic void onClick(View arg0) { //没有bindService之前调用会抛异常,因此调用前需要判断是否绑定ServiceAnalysis.this.unbindService(sc);}});interact.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) { //只要之前调用过Bind(即使又调用了Unbind),就可以调用服务中的方法Toast.makeText(ServiceAnalysis.this, "从Service取到的整数为 "+analysedService.getRandomInt(), Toast.LENGTH_SHORT).show();}});lastThing.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) { Intent intent = new Intent(AnalysedService.AUTO_STOP); intent.setClass(ServiceAnalysis.this, AnalysedService.class); ServiceAnalysis.this.startService(intent);}});}}
更多相关文章
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用
- python list.sort()根据多个关键字排序的方法实现
- android定制对话框
- android中drawable转bitmap的两种方法
- Android(安卓)中实现FlowLayout 布局
- Android(安卓)子线程 更新 UI 界面 总结
- android的activitygroup使用
- Android三种实现定时器的方法