1. 修改MainActivity.java

package ai.nixie.smartmailbox;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;public class MainActivity extends AppCompatActivity implements OnMailDelivery{    SmartMailbox smartMailbox;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        smartMailbox = new SmartMailbox(this);    }    @Override    public int onMailDelivery() {        //邮递员来送信了的时候触发此事件        Log.e("Mailman", "I just delivered a few mails to your mailbox!");        return 3;    }    public void button1(View view) {        smartMailbox.StartMonitoring();    }}

2. 新建一个Interface文件OnMailDelivery.java

package ai.nixie.smartmailbox;/** * Created by aidenfang on 4/5/18. */public interface OnMailDelivery {    int onMailDelivery();}

3. 新建一个SmartMailbox

package ai.nixie.smartmailbox;import android.util.Log;/** * Created by aidenfang on 4/5/18. */public class SmartMailbox {    OnMailDelivery robot;    public SmartMailbox(OnMailDelivery listener) {        this.robot = listener;    }    public void StartMonitoring() {        Log.e("NOTE: ", "Monitoring Started!");        try {            Thread.sleep(3000);        } catch (InterruptedException e) {            e.printStackTrace();        }        if (robot.onMailDelivery()>0) {            Log.e("NEW MAIL ALERT ->>", "You have " + robot.onMailDelivery() + " new mails");        }else {            Log.e("NEW MAIL ALERT ->>", "You don't have mails!");        }    }}

4. 修改res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>    

5. 运行

点击按钮,查看logcat.

E/NOTE:: Monitoring Started!E/Mailman: I just delivered a few mails to your mailbox!E/Mailman: I just delivered a few mails to your mailbox!E/NEW MAIL ALERT ->>: You have 3 new mails

更多相关文章

  1. 期中实验:记事本实现时间戳、搜索、正文缩略显示
  2. 修改状态栏颜色和状态栏字体颜色2
  3. Android(安卓)官方屏幕适配之ScreenMatch
  4. android 平台USB wifi驱动移植及使用
  5. Android(安卓)开发之 ---- bootloader (LK)&&android lk bootloade
  6. Android(安卓)为【apk】文件签名,增加修改系统时间等权限
  7. Android(安卓)Jetpack 组件之 ViewModel(Kotlin)
  8. 解决Android(安卓)sdk manager无法访问google服务器更新的问题
  9. Android(安卓)默认AP名字,以及AP名字存储路径

随机推荐

  1. android AndroidManifest.xml 权限示例
  2. Android实现TextView动画缩放
  3. androidの布局控件居于最底部实现
  4. Android有进度条的下载图片并且显示图片
  5. Android(安卓)TimePicker
  6. Android相关sdk使用
  7. Android中按钮点击效果显示
  8. Android预定义样式
  9. Activity的启动模式
  10. 2011.09.01——— android 透明显示