Android中BroadcastReceiver案例讲解

前言

我们在注册一款App的时候,如果注册成功的话,那么我们就直接跳过登陆界面,直接进入主界面,那么我们现在就通过BroadcastReceiver来实现这个功能:

(1)注册界面RegisterActivity.java,这里跳过了一些基本的操作,直接调用的是注册方法signUp()方法:

btn_reg.signUp(RegisterActivity.this, new SaveListener() {
      @Override
      public void onSuccess() {
        progress.dismiss();
        ShowToast("注册成功");
        /** 注册成功,发广播通知登陆页面退出  **/
        sendBroadcast(new Intent(Constants.ACTION_REGISTER_SUCCESS_FINISH));
        // 启动主页面
        Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
        startActivity(intent);
        finish();
      }
      @Override
      public void onFailure(int arg0, String arg1) {
        BmobLog.i(arg1);
        ShowToast("注册失败:" + arg1);
        progress.dismiss();
      }
    });

我们可以看到,已经发送了一个广播,广播名为:Constants.ACTION_REGISTER_SUCCESS_FINISH)

(2)如何通知登陆界面哪?下边就是登陆界面的实现:

我们需要在登陆界面的onCreate方法中进行动态注册一个广播接受者:

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    //动态注册一个广播接受者
    //注册退出广播:就是一开始注册的时候,注册成功的话就退出登陆界面,直接进入主界面
    IntentFilter filter = new IntentFilter();
    // 注册成功之后登陆页面退出
    // public static final String ACTION_REGISTER_SUCCESS_FINISH ="register.success.finish";
    filter.addAction(BmobConstants.ACTION_REGISTER_SUCCESS_FINISH);
    registerReceiver(receiver, filter); //注册进去
  }

另外值得注意的是,我们通过动态注册的一个广播接受者的haunted,我们就需要在onDestroy()方法中接触绑定:

@Override
  protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(receiver);
  }

(3)下边是自己定义的一个广播接受者的类,用于匹配意图的时候,将login界面finish:

  public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
      //如果匹配的话,就执行finnish()
      if (intent != null && BmobConstants.ACTION_REGISTER_SUCCESS_FINISH.equals(intent.getAction())) {
        finish(); //注册成功之后,将登陆界面finish()
      }
    }
  }

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接

(0)

相关推荐

  • Android开发实现ListView点击展开收起效果示例

    本文实例讲述了Android开发实现ListView点击展开收起效果.分享给大家供大家参考,具体如下: 废话不说先上效果: 实际上这是采用一个ExpandableListView实现的 布局文件很简单: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xm

  • Android实现手机震动抖动效果的方法

    Android手机震动抖动效果的实现 (1)布局文件如下 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_p

  • Android中Gallery和ImageSwitcher的使用实例

    效果如下: 布局文件activity_main.xml如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match

  • Android列表组件ListView使用详解之动态加载或修改列表数据

    在使用ListView组件来显示列表数据时,有的时候我们需要改变列表中的数据,有以下方法: 1.重新给ListView组件设置适配器 这种方法重新创建了ListView,效率不好. 2.使用适配器中的方法 /** * Notifies the attached observers that the underlying data has been changed * and any View reflecting the data set should refresh itself. */ pu

  • Android侧滑菜单之DrawerLayout用法详解

    onConfigurationChanged最早的时候实现侧滑菜单功能大多时候需要通过github上一个叫做SlidingMenu的开源通过依赖包来实现,后来谷歌在v4包中添加了DrawerLayout来实现这个功能,完全可以替代SlidingMenu,这里我们来学习DrawerLayout的用法 一)创建DrawerLayout 1)在布局文件里将布局设置为DrawerLaout,而且因为是v4包中的功能,所以必须写全包名,注意第一必须先写主视图布局,然后再写抽屉里的视图,这里我们放了List

  • Android开源框架的SlidingFragment的使用示例

    效果如下: 直接上代码,留着以后用,代码目录结构如下: 其中BaseFragment.java是另外5个Fragment的基类,代码如下: package com.xuliugen.newsclient.fragment.base; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; impor

  • Android编程程序实现一键锁屏的方法讲解

    Android程序之一键锁屏 (1)布局文件activity_main.xml如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height

  • Android手机屏幕px与dp互转的工具类

    dp(dip): device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖像素.dp也就是dip,这个和sp基本类似.如果设置表示长度.高度等属性时可以使用dp 或sp.但如果设置字体,需要使用sp.dp是与密度无关,sp除了与密度无关外,还与scale无关.如果屏幕密度为160,这时dp和sp和px是一 样的.1dp=1sp=1px,但如果使用px作单位,如果屏幕大小不

  • Android实现电子罗盘(指南针)方向传感器的应用

    简介 现在每部Android手机里边都会内置有许多传感器,如光照传感器.加速度传感器.地磁传感器.压力传感器.温度传感器等,它们能够监测到各种发生在手机撒花姑娘的物理事件.当然Android系统只是负责将这些传感器所输出的信息传递给我们,然后我们可以利用这些信息去开发一些好玩的应用. 图片神马的在网上搜个指南针图片就好了,方便学习 main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo

  • Android实现手机定位的案例代码

    Android手机定位案例代码 代码如下: package com.xuliugen.gpsdemo; import com.itheima.gpsdemo.R; import android.app.Activity; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.Locat

随机推荐