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