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

Android广播接收机制详细介绍(附短信接收实现)

程序员文章站 2022-06-04 12:07:00
android中广播(broadcastreceiver)的详细讲解. 1. broadcastreceiver的注册过程: (1).广播消息发出来后,只有订阅了该广...

android中广播(broadcastreceiver)的详细讲解.

1. broadcastreceiver的注册过程:
(1).广播消息发出来后,只有订阅了该广播的对象才会接收发出来的广播消息并做出相应处理。
**(2).**android广播分为两个方面:广播发送者和广播接收者。android中的广播使用了观察者模式,基于消息的发布/订阅事件模型。广播接收者通过binder机制向ams进行注册,ams查找符合相应条件的broadcastreceiver,将广播发送到broadcastreceiver(一般情况下是activity)相应的消息循环队列中。消息循环执行拿到此广播,回调broadcastreceiver中的onreceive()方法。
(3).广播发送者和广播接收者的执行顺序是异步的,发出去的广播不会关心有无接收者的接收,也不确定接收者是什么时候能接收到。
2. broadcastreceiver的步骤:
(1).注册广播:广播接收者在ams中注册广播。
(2).发送广播:广播发送者向ams中发送广播。
(3).接收广播:广播接收者拿到广播后调用onreceive()方法执行。

以下是一个发送短信的例子,代码如下:

broadcastreceiverhelper 类:

package com.scd.broadcastreceiver.helper;

import com.scd.broadcastreceiver.activity.mainactivity;

import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.widget.toast;

/**
 * 广播接收
 * 
 * @author scd
 * 
 */
public class broadcastreceiverhelper extends broadcastreceiver {
  /** 上下文 */
  private mainactivity mactivity = null;

  public broadcastreceiverhelper(mainactivity mcontext) {
    super();
    mactivity = mcontext;
  }

  @override
  public void onreceive(context context, intent intent) {
    if (intent.getaction().equals(mainactivity.action_sendmessage)) {
      toast("发送成功");
    } else if (intent.getaction()
        .equals(mainactivity.action_delivermessage)) {
      toast("接收成功");
    }

  }

  public void toast(string text) {
    toast.maketext(mactivity, text, toast.length_short).show();

  }

}


mainactivity类:

package com.scd.broadcastreceiver.activity;

import com.scd.broadcastreceiver.r;
import com.scd.broadcastreceiver.helper.broadcastreceiverhelper;

import android.app.activity;
import android.app.pendingintent;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.content.intentfilter;
import android.os.bundle;
import android.telephony.gsm.smsmanager;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.toast;

public class mainactivity extends activity implements onclicklistener {
  private button mbutton = null;
  /** 发送短信 */
  public static string action_sendmessage = "com.scd.broadcastreceiver.mainactivity.sendmessage";
  /** 接收短信 */
  public static string action_delivermessage = "com.scd.broadcastreceiver.mainactivity.delivermessage";
  /** reciver类 */
  private broadcastreceiverhelper mhelpers = null;
  private broadcastreceiverhelper mhelperd = null;
  /** 短信管理类 */
  private smsmanager msmsmanager = null;

  /** 电话号码 */
  private string mtelephone = "13607567010";

  /** 短信内容 */
  private string mcontext = "你好,有什么需要帮助的吗?";

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);

    mbutton = (button) findviewbyid(r.id.sendbroadcast);
    mbutton.setonclicklistener(this);
    msmsmanager = smsmanager.getdefault();
    // 注册广播
    registerbroadcast();
  }

  @override
  public void onclick(view v) {
    switch (v.getid()) {
    case r.id.sendbroadcast: {
      //发送广播
      sendbroadcastreceiver(mtelephone, mcontext);
      break;
    }

    default:
      break;
    }

  }

  /**
   * 注册广播
   */
  public void registerbroadcast() {
    // 发送
    intentfilter intentfilters = new intentfilter(action_sendmessage);
    mhelpers = new broadcastreceiverhelper(mainactivity.this);
    this.registerreceiver(mhelpers, intentfilters);
    // 接收
    intentfilter intentfilterd = new intentfilter(action_delivermessage);
    mhelperd = new broadcastreceiverhelper(mainactivity.this);
    this.registerreceiver(mhelperd, intentfilterd);
  }

  /**
   * 发送广播 短信发送的过程:发送的时候需要发送广播一次,接收的时候需要发送广播一次
   */
  public void sendbroadcastreceiver(string telephone, string content) {
    if (telephone != null) {
      intent sintent = new intent(action_sendmessage);
      // 短信成功发送后才发送该广播
      pendingintent sentintent = pendingintent.getbroadcast(
          mainactivity.this, 0, sintent, 0);

      intent dintent = new intent(action_delivermessage);
      // 短信接收后才发送该广播
      pendingintent deliveryintent = pendingintent.getbroadcast(
          mainactivity.this, 0, dintent, 0);
      msmsmanager.sendtextmessage(telephone, null, content, sentintent,
          deliveryintent);
      // this.sendbroadcast(intent);

    }

  }
}

注意:需要添加以下权限:

 

 <!-- 短信权限 -->
  <uses-permission android:name="android.permission.send_sms" >
  </uses-permission>
  <uses-permission android:name="android.permission.read_sms" >
  </uses-permission>
  <uses-permission android:name="android.permission.receive_sms" >
  </uses-permission>