多态在android中(利用接口调用服务中方法)的应用

首先我们已经了解了java中多态的基本概念
1方法重载。
2向上转型,向下转型

今天我们主要讲向上转型在android中的应用范例。
在java中向上向下转型的图解:
在这里插入图片描述
重点:子类对象向上转型为父类对象后,可以调用父类中的方法和子类中已经覆写过的父类方法。子类中新定义的方法无法访问
在android中,有这样一个需求,我需要通过bindService的方式从MainActivity访问服务中的方法,但是有的方法服务想让MainActivity进行访问,有的方法不想让MainActivity访问。
在下面的图解中,人民想要访问派出所中的办证方法,因为直接实例化服务对象调用没有上下文参数,是实现不了的,所以我们需要定义一个中间人来访问办证方法。
这个中间人能办证,陪领导打麻将,陪领导洗桑拿三个方法。
在这里插入图片描述
MainActivity:

package com.example.banzheng;


import com.example.banzheng2.R;
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.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

	private myConn conn;
	private Iservice myBinder;//定义的中间人对象   接口类型
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Intent intent=new Intent(this,DemoService.class);
		conn = new myConn();
		bindService(intent,conn, BIND_AUTO_CREATE);
	}

	//点击按钮调用服务里面办证的方法
	public void click(View v){
		myBinder.callBanZheng(10);
		
	}
	
	//监视服务的状态
	private class myConn implements ServiceConnection{

		   
		//当服务连接成功的时候调用
			@Override
			public void onServiceConnected(ComponentName name, IBinder service) {
				myBinder = (Iservice)service;//向上转型为接口类型,该对象只能调用父接口中已被子类实现的方法,而在子类中三个方法已经全部“实现”,所以在接口中定义需要被覆写的方法就是该接口允许MainActivity访问的方法。
				
				
			}
			//失去连接
			@Override
			public void onServiceDisconnected(ComponentName name) {
				// TODO Auto-generated method stub
				
			}
		}
	
	@Override
	protected void onDestroy() {
		//当activity销毁的时候解绑对象
		unbindService(conn);
		super.onDestroy();
	}
}

DemoService

package com.example.banzheng;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

public class DemoService extends Service {

	//把我定义的中间人对象返回
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return new MyBinder();
	}
	
	//办证的方法
	public void banZheng(int money){
		if(money>1000){
			Toast.makeText(getApplicationContext(), "我是领导,把证给你办了", 1).show();
		}else{
			Toast.makeText(getApplicationContext(), "这点钱,还想办事。。。", 1).show();
		}
	}
	
		//打麻将的方法
		public void playMaJiang(){
			System.out.println("陪领导打麻将");
		}
	    //洗桑拿的方法
		public void 洗桑拿(){
			System.out.println("陪领导洗桑拿");
		}
	
	//【1】定义中间人对象
	private class MyBinder extends Binder implements Iservice{//让MyBinder实现 Iservice接口
		public void callBanZheng(int money){
			//调用办证的方法
			banZheng(money);
		}
		
		public void callplayMaJiang(){
			//调用打麻将的方法
			playMaJiang();
		}
		
		public void callxiSangNa(){
			//调用洗桑拿的方法
			洗桑拿();
		}
	}

}

Iservice(从这里定义Service想要暴露给MainActivity的方法)

package com.example.banzheng;

public interface Iservice {

	//把领导想暴露的方法定义在接口里
	public void callBanZheng(int money);
	//人民只能调办证的方法。陪领导打麻将洗桑拿只能领导(服务)自己调用。
}

发布了54 篇原创文章 · 获赞 17 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_43311389/article/details/82975599
今日推荐