Android学习之本地广播使用方法详解

 更新时间:2017年08月09日 10:06:26   作者:天秤心已随风去  
这篇文章主要为大家详细介绍了Android学习之本地广播使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本地广播信息只能在应用程序内部传递,同时广播接收器也只能接收应用程序内部的广播消息。

MainActivity代码

package com.example.luobo.mybroadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

  private Button button;
  private IntentFilter intentFilter;
  private LocalBroadcastManager localBroadcastManager ;
  private LocalReceiver localReciiver;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button)findViewById(R.id.send_button);
    button.setOnClickListener(this);
    localBroadcastManager = LocalBroadcastManager.getInstance(this);//使用
    intentFilter = new IntentFilter();
    intentFilter.addAction("com.example.luobo.mybroadcastreceiver.LOCAL_BROADCAST");
    localReciiver = new LocalReceiver();
    localBroadcastManager.registerReceiver(localReciiver,intentFilter);
  }

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

  @Override
  public void onClick(View view) {
    Intent intent = new Intent("com.example.luobo.mybroadcastreceiver.LOCAL_BROADCAST");
    localBroadcastManager.sendBroadcast(intent);
  }
  class LocalReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
      Toast.makeText(context,"received local broadcast",Toast.LENGTH_SHORT).show();
    }
  }
}

首先通过LocalBroadcastManager(本地广播管理类)的getInstance(this)方法获取实例,注册广播消息时是调用localBroadcastManager实例的registerReceiver(参数1,参数2)方法注册(参数1是本地广播接受者,参数2是过滤器只选择接收特定的广播消息),调用localBroadcastManager实例的sendBroadcast(Initent initent)方法发送广播消息。

MyRecevity

package com.example.luobo.mybroadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    Toast.makeText(context,"Received in MyBroadCastReceiver",Toast.LENGTH_SHORT).show();
    abortBroadcast();
  }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.luobo.mybroadcastreceiver.MainActivity">
  <Button
    android:id="@+id/send_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="发送广播"/>

</android.support.constraint.ConstraintLayout>

AndroidMainfest.aml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.luobo.mybroadcastreceiver">

  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>

    <receiver
      android:name=".MyReceiver"
      android:enabled="true"
      android:exported="true">
      <intent-filter
        android:priority="100">
        <action android:name="com.example.luobo.mybroadcastreceiver.LOCAL_BROADCAST"/>
      </intent-filter>
    </receiver>
  </application>

</manifest>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android 中ScrollView与ListView冲突问题的解决办法

    Android 中ScrollView与ListView冲突问题的解决办法

    这篇文章主要介绍了Android 中ScrollView与ListView冲突问题的解决办法的相关资料,希望通过本文能帮助到大家,让大家掌握解决问题的办法,需要的朋友可以参考下
    2017-10-10
  • Android 简单跳转页面工具的实例详解

    Android 简单跳转页面工具的实例详解

    这篇文章主要介绍了Android 简单跳转页面工具的实例详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • Android生成二维码工具类封装及使用

    Android生成二维码工具类封装及使用

    最近公司业务上有个生成二维码图片的需求(Android端),之后笔者在网上查阅了一些资料,实现了这个功能,这篇文章主要给大家介绍了关于Android生成二维码工具类封装及使用的相关资料,需要的朋友可以参考下
    2024-04-04
  • 浅扒Android动态设置字体大小的示例

    浅扒Android动态设置字体大小的示例

    本篇文章主要介绍了浅扒Android动态设置字体大小的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • Android使用贝塞尔曲线画心形

    Android使用贝塞尔曲线画心形

    这篇文章主要为大家详细介绍了Android使用贝塞尔曲线画心形,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • TableLayout(表格布局)基础知识点详解

    TableLayout(表格布局)基础知识点详解

    在本文里我们给大家分享了关于TableLayout(表格布局)的相关基础知识点内容,需要的朋友们学习下。
    2019-02-02
  • Android 让自定义TextView的drawableLeft与文本一起居中

    Android 让自定义TextView的drawableLeft与文本一起居中

    本文主要介绍Android 自定义控件TextView显示居中问题,在开发过程中经常会遇到控件的重写,这里主要介绍TextView的drawableLeft与文本一起居中的问题
    2016-07-07
  • Android Bitmap压缩方法的选择详解

    Android Bitmap压缩方法的选择详解

    这篇文章主要介绍了Android Bitmap压缩方法的选择的相关资料,需要的朋友可以参考下
    2016-09-09
  • iOS UIButton 点击无响应的解决办法

    iOS UIButton 点击无响应的解决办法

    在开发中按钮我们经常会遇到,但是有时候会碰到一些难以处理的问题,就是按钮点击无响应,其实解决方法也不难。下面小编之家小编抽空给大家介绍iOS UIButton 点击无响应的解决办法,需要的朋友参考下吧
    2017-12-12
  • Android webview与js的数据交互

    Android webview与js的数据交互

    有了WebView这个组件,Android应用开发技术也就转嫁到html与java数据交互上来。说白了就是js与WebView的数据交互,这就是本文所要讨论的
    2017-04-04

最新评论