Android 对接口的封装使用

前言

本篇文章主要是记录Android代码 对java 接口的封装和使用方法,比较基础,记录一下,阅读本篇文章前,请移步java基础系列(九) 接口和抽象类 这篇文章。

接口理解

从设计角度: 设计方面的区别

抽象类是对一种事物的抽象,即对类的抽象;而接口是对行为的抽象。抽象类是对整个类整体进行抽象,包括属性、行为;接口是对类局部(行为)进行抽象。

比如:飞机和鸟不是同类的事物,但是它们都有一个共性---会飞。那么在设计的时候,可以将飞机设计为一个类Plane,将鸟设计为一个类Bird,但是不能将飞行这个特性也设计为一个类,因为它只是一个行为,并不是对一类事物的抽象描述。此时,可以将飞行设计为一个接口Fly,包含方法fly(),然后Plane和Bird分别根据自己的需要实现Fly这个接口。

然后至于有不同种类的飞机,比如战斗机、民用飞机等直接继承Plane即可,对于鸟类也是类似的,不同种类的鸟直接继承Bird类即可。

(从这个例子中能看出,继承是一个"是不是"的关系,而接口实现则是"有没有、具备不具备"的关系)

示例

工程结构: 就三个文件

定义了两种 接口类型,一个MyInterface文件, 一个在Listener.java文件中定义内部接口

代码如下:

Listener.java

java 复制代码
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class Listener {

    private Context mContext;
    private IntentFilter mHomeBtnIntentFilter;
    private OnHomeBtnPressLitener mOnHomeBtnPressListener;
    private HomeBtnReceiver mHomeBtnReceiver;

    private MyInterface myInterfaceTest;


    public Listener(Context context) {
        mContext = context;
        mHomeBtnReceiver = new HomeBtnReceiver( );
        mHomeBtnIntentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    }

    public void setOnHomeBtnPressListener( OnHomeBtnPressLitener onHomeBtnPressListener ){
        mOnHomeBtnPressListener = onHomeBtnPressListener;
    }

    public void setHelloGoodByeListener(MyInterface myInterface) {
        myInterfaceTest = myInterface;
    }

    public void start( ){
        mContext.registerReceiver(mHomeBtnReceiver, mHomeBtnIntentFilter);
    }


    public void stop( ){
        mContext.unregisterReceiver(mHomeBtnReceiver);
    }

    class HomeBtnReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            receive( context, intent );
        }
    }

    private void receive(Context context, Intent intent){
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
            //按下home键处理实现接口中的事件
            mOnHomeBtnPressListener.onHomeBtnPress( );
            mOnHomeBtnPressListener.onHomeBtnLongPress();

            myInterfaceTest.sayHello("你好 各位美女帅哥");
            myInterfaceTest.sayGoodbye();
        }
    }


   //在Listener.java 文件中定义的内部接口
    public interface OnHomeBtnPressLitener{
        public void onHomeBtnPress( );
        public void onHomeBtnLongPress( );
    }
}
复制代码
MyInterface.java文件
java 复制代码
public interface MyInterface {
    void sayHello(String str);

    void sayGoodbye();

}

主调用文件 MainActivity.java

java 复制代码
public class MainActivity extends AppCompatActivity {


    private Listener mListen = null;
    private Listener.OnHomeBtnPressLitener listener;

    private MyInterface myInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listener = new Listener.OnHomeBtnPressLitener() {
            @Override
            public void onHomeBtnPress() {
                showToast( "按下Home按键!" );
            }

            @Override
            public void onHomeBtnLongPress() {
                showToast( "长按Home按键!" );
            }
        };

        myInterface = new MyInterface() {
            @Override
            public void sayHello(String test) {
                Toast.makeText( MainActivity.this, test, Toast.LENGTH_LONG).show( );
            }

            @Override
            public void sayGoodbye() {
                Toast.makeText(MainActivity.this, "拜拜", Toast.LENGTH_LONG).show();
            }
        };

        initListen( );
    }

    @Override
    protected void onResume( ) {
        super.onResume();
        mListen.start( );
    }


    @Override
    protected void onPause() {
        super.onPause();
        mListen.stop( );
    }

    private void initListen( ){
        mListen = new Listener( this );
        mListen.setOnHomeBtnPressListener(listener);
        mListen.setHelloGoodByeListener(myInterface);
    }

    private void showToast( String toastInfoStr ){
        Toast.makeText( this, toastInfoStr, Toast.LENGTH_LONG ).show( );
    }
}
相关推荐
雮尘13 分钟前
Android性能优化之枚举替代
android
2501_915909062 小时前
苹果上架App软件全流程指南:iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传与审核技巧详解
android·ios·小程序·https·uni-app·iphone·webview
2501_915921432 小时前
iOS 文件管理与能耗调试结合实战 如何查看缓存文件、优化电池消耗、分析App使用记录(uni-app开发与性能优化必备指南)
android·ios·缓存·小程序·uni-app·iphone·webview
2501_915918413 小时前
App 苹果 上架全流程解析 iOS 应用发布步骤、App Store 上架流程
android·ios·小程序·https·uni-app·iphone·webview
2501_916007473 小时前
苹果上架全流程详解,iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传与审核要点完整指南
android·ios·小程序·https·uni-app·iphone·webview
PuddingSama4 小时前
Android 高级绘制技巧: BlendMode
android·前端·面试
2501_915921434 小时前
iOS App 性能监控与优化实战 如何监控CPU、GPU、内存、帧率、耗电情况并提升用户体验(uni-app iOS开发调试必备指南)
android·ios·小程序·uni-app·iphone·webview·ux
Digitally5 小时前
如何将视频从安卓手机传输到电脑?
android·智能手机·电脑
CV资深专家5 小时前
Android 相机框架的跨进程通信架构
android
前行的小黑炭5 小时前
Android :如何提升代码的扩展性,方便复制到其他项目不会粘合太多逻辑,增强你的实战经验。
android·java·kotlin