该课时开始讲解GlobalKey一键启动程序,一键启动的过程如下: a.对于GlobalKey,系统会根据global_key.xml文件决定发送消息给那个组件 b.APP应该注册广播消息的接收者 1.编写一个BroadCastReceiver派生类,实现消息的处理函数 2.注册派生类 c.在该组件中启动APP 首先我们实现a,b两点,先写出一个能接收广播消息的应用程序,、
APP应用程序我们先来实现上述的b,然后再实现a。
APP修改
我们在之前的APP_0001_LEDDemp-V3上进行修改,使用AS进入该工程,点击:
弹出如下窗口:
给需要创建的类命名为MyBroadcastReceiver,可以看到生成了一个新的文件,其内容为下:
public class MyBroadcastReceiver { }
我们让该类继承于BroadcastReceiver,然后我们需要复写其成员onReceive,简单修改如下:
public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context,"Get BroadcasstReceiver",Toast.LENGTH_SHORT).show(); } }
当接收到广播的时候会执行onReceive函数,Toast.makeText(context,“Get BroadcasstReceiver”,Toast.LENGTH_SHORT).show()为简单的显示一个提示框,其内容为"Get BroadcasstReceiver",下面我们修改app-> manifests-> AndroidManifest.xml文件,注册该APP为一个静态广播接收者,在
关注
打赏