Android--Service生命周期(二)

来源:互联网 发布:手机淘宝怎么改成好评 编辑:程序博客网 时间:2024/05/22 10:07

修改Android--Service生命周期(一)中的代码,来实现与Service通讯:

package com.service.lifecycle.demo;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.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;public class ServiceLifeCycleActivity extends Activity {private MyServiceConnection conn;private IService mIService;private TextView tv;private Button getStringBtn;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Log.d(AppConstant.TAG, "FirstActivity-->onCreate()");conn = new MyServiceConnection();Button startServiceBtn = (Button) findViewById(R.id.startServiceBtn);Button endServiceBtn = (Button) findViewById(R.id.endServiceBtn);Button bindServiceBtn = (Button) findViewById(R.id.bindServiceBtn);Button unBindServiceBtn = (Button) findViewById(R.id.unBindServiceBtn);getStringBtn = (Button) findViewById(R.id.getStringBtn);tv = (TextView) findViewById(R.id.tv);getStringBtn.setEnabled(false);startServiceBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(ServiceLifeCycleActivity.this, ServiceDemo.class);ServiceLifeCycleActivity.this.startService(intent);}});endServiceBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(ServiceLifeCycleActivity.this, ServiceDemo.class);ServiceLifeCycleActivity.this.stopService(intent);}});bindServiceBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(ServiceLifeCycleActivity.this, ServiceDemo.class);ServiceLifeCycleActivity.this.bindService(intent, conn, ServiceLifeCycleActivity.this.BIND_AUTO_CREATE);getStringBtn.setEnabled(true);}});unBindServiceBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {ServiceLifeCycleActivity.this.unbindService(conn);getStringBtn.setEnabled(false);tv.setText("");}});getStringBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String result = mIService.getYourMethod(100);tv.setText(result);}});}private final class MyServiceConnection implements ServiceConnection {@Overridepublic void onServiceConnected(ComponentName arg0, IBinder arg1) {Log.d(AppConstant.TAG, "建立了和Service通讯的通道");mIService = (IService) arg1;}@Overridepublic void onServiceDisconnected(ComponentName arg0) {Log.d(AppConstant.TAG, "关闭了和Service通讯的通道");mIService = null;}}@Overrideprotected void onStart() {super.onStart();Log.d(AppConstant.TAG, "FirstActivity-->onStart()");}@Overrideprotected void onRestart() {super.onRestart();Log.d(AppConstant.TAG, "FirstActivity-->onRestart()");}@Overrideprotected void onResume() {super.onResume();Log.d(AppConstant.TAG, "FirstActivity-->onResume()");}@Overrideprotected void onPause() {super.onPause();Log.d(AppConstant.TAG, "FirstActivity-->onPause()");}@Overrideprotected void onStop() {super.onStop();Log.d(AppConstant.TAG, "FirstActivity-->onStop()");}@Overrideprotected void onDestroy() {super.onDestroy();Log.d(AppConstant.TAG, "FirstActivity-->onDestroy()");}}

package com.service.lifecycle.demo;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class ServiceDemo extends Service {private Binder binder = new MyBinder();@Overridepublic void onCreate() {super.onCreate();Log.d(AppConstant.TAG, "ServiceDemo-->onCreate()");}@Overridepublic IBinder onBind(Intent arg0) {Log.d(AppConstant.TAG, "ServiceDemo-->onBind()");return binder;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d(AppConstant.TAG, "ServiceDemo-->onStartCommand()");try{Thread.sleep(5000);} catch (Exception e) {Log.d(AppConstant.TAG, "ServiceDemo-->onStartCommand()中Thread.sleep出现异常");} finally {this.stopSelf();Log.d(AppConstant.TAG, "ServiceDemo-->onStartCommand()中执行了stopSelf()");}return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {super.onDestroy();Log.d(AppConstant.TAG, "ServiceDemo-->onDestroy()");}public String getYourMethod(int id) {String result = "";switch (id) {case 100:result = "Your String is 100";break;default:result = "default";break;}return result;}private final class MyBinder extends Binder implements IService{public String getYourMethod(int id) {return ServiceDemo.this.getYourMethod(id);}}}

package com.service.lifecycle.demo;public interface IService {public String getYourMethod(int id);}

运行如图:

点击“bind Service”按钮:

点击“getString”按钮:

点击“unBind Service”按钮:



原创粉丝点击