亲宝软件园·资讯

展开

Android广播接收机制 Android广播接收机制仔细讲解(附短信接收实现)

人气:0
想了解Android广播接收机制仔细讲解(附短信接收实现)的相关内容吗,在本文为您仔细讲解Android广播接收机制的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,广播接收机制,下面大家一起来学习吧。

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>

加载全部内容

相关教程
猜你喜欢
用户评论