欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-16 20:28:40
本地广播信息只能在应用程序内部传递,同时广播接收器也只能接收应用程序内部的广播消息。 mainactivity代码 package com.example.lu...

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

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>

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