Android--Service生命周期(二)

修改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. */
	@Override
	public 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() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ServiceLifeCycleActivity.this, ServiceDemo.class);
				ServiceLifeCycleActivity.this.startService(intent);
			}
		});
		endServiceBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ServiceLifeCycleActivity.this, ServiceDemo.class);
				ServiceLifeCycleActivity.this.stopService(intent);
			}
		});
		
		bindServiceBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public 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() {
			
			@Override
			public void onClick(View v) {
				ServiceLifeCycleActivity.this.unbindService(conn);
				getStringBtn.setEnabled(false);
				tv.setText("");
			}
		});
		getStringBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String result = mIService.getYourMethod(100);
				tv.setText(result);
			}
		});
	}
	
	private final class MyServiceConnection implements ServiceConnection {

		@Override
		public void onServiceConnected(ComponentName arg0, IBinder arg1) {
			Log.d(AppConstant.TAG, "建立了和Service通讯的通道");
			mIService = (IService) arg1;
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			Log.d(AppConstant.TAG, "关闭了和Service通讯的通道");
			mIService = null;
		}
		
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.d(AppConstant.TAG, "FirstActivity-->onStart()");
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		Log.d(AppConstant.TAG, "FirstActivity-->onRestart()");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(AppConstant.TAG, "FirstActivity-->onResume()");
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d(AppConstant.TAG, "FirstActivity-->onPause()");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.d(AppConstant.TAG, "FirstActivity-->onStop()");
	}

	@Override
	protected 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();
	
	@Override
	public void onCreate() {
		super.onCreate();
		Log.d(AppConstant.TAG, "ServiceDemo-->onCreate()");
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		Log.d(AppConstant.TAG, "ServiceDemo-->onBind()");
		return binder;
	}
	
	@Override
	public 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);
	}
	
	@Override
	public 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);
}

运行如图: Android--Service生命周期(二)_第1张图片

点击“bind Service”按钮:Android--Service生命周期(二)_第2张图片Android--Service生命周期(二)_第3张图片

点击“getString”按钮:Android--Service生命周期(二)_第4张图片Android--Service生命周期(二)_第5张图片

点击“unBind Service”按钮:Android--Service生命周期(二)_第6张图片



你可能感兴趣的:(exception,String,service,Class,button,通讯)